English 中文(简体)
Java TCP简单的通信带来了意外结果
原标题:Java TCP simple communication gives unexpected results
  • 时间:2011-10-30 16:20:55
  •  标签:
  • java
  • tcp
  • hex

I'm试图在我的java方案中将一些简单的数据寄给TCP:

         String data = "70798090999a9b9c9d9e9fa0a1a2";
             ServerSocket srvr = new ServerSocket(1234);
             Socket skt = srvr.accept();
             OutputStream out = skt.getOutputStream();
             out.write(hexStringToByteArray(data));
             out.close();
             skt.close();
             srvr.close();

职能:

public static byte[] hexStringToByteArray(String s) {
    int len = s.length();
    byte[] data = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                             + Character.digit(s.charAt(i+1), 16));
    }
    return data;
}

接收数据如下:

     Socket skt = new Socket("localhost", 1234);
     BufferedReader in = new BufferedReader(new
        InputStreamReader(skt.getInputStream()));

     while (!in.ready()) {}
     while (in.ready()) {
     System.out.println(in.read()); // Read one line and output it
     }

但是,我拿不出一个通常会增加人数的清单,而是会随意增加和减少:

121
196
234
244
246
245
250
249
251
252
8224
176
162

我在什么地方做过错?

最佳回答

如果你想寄出案文,我建议你读一下。 你们不要试图混淆和相配,或者你可能会突然混淆。

byte[] bytes = new BigInteger("70798090999a9b9c9d9e9fa0a1a2", 16).toByteArray();

ServerSocket ss = new ServerSocket(1234);
Socket c = new Socket("localhost", 1234);
Socket s = ss.accept();
final OutputStream out = s.getOutputStream();
out.write(bytes.length);
out.write(bytes);

final DataInputStream in = new DataInputStream(c.getInputStream());
int length = in.read();
byte[] bytes2 = new byte[length];
in.readFully(bytes2);

System.out.println(new BigInteger(1, bytes2).toString(16));
c.close();
s.close();
ss.close();

印刷

70798090999a9b9c9d9e9fa0a1a2
问题回答

你们只是派人,只读一次。

你们必须阅读一下,像以下那样:

byte[] arr = new byte[1024];
while (in.read(arr) >= 0) {
    // do something with the data.
}

这将发挥作用,但这不是你真正需要的东西。 确实,你以身作则,然后手工改写,然后用斜体读取,以手工方式将其转化成活体?

相反,你应使用印刷机撰写插播:

PrintWriter writer = new PrintWriter(new OutputStreamWriter(out));
writer.write(str);

然后在阅读数据时使用BufferedReader。

BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = reader.readLine();

排除所有准备就绪的测试。 原文在数据到达之前将予以阻挡,这样,可以随时随地打断时间,并有可能丢失数据。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签