简体   繁体   English

Java TCP简单通信带来了意想不到的结果

[英]Java TCP simple communication gives unexpected results

I`m trying to send some simple data over TCP in my java program: 我正在尝试在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();

With function: 具有功能:

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;
}

And receive the data as: 并以以下方式接收数据:

     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
     }

But instead of receiving a list of numbers that increment themselves normally, I get something that increases and decreases at whim: 但是我没有收到通常会递增的数字列表,而是随心所欲地增加和减少了一些东西:

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

Where am I doing something wrong? 我在哪里做错了什么?

You are sending byte array and reading one byte only. 您正在发送字节数组,并且仅读取一个字节。

You have to read using loop like the following: 您必须阅读using循环,如下所示:

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

This will work but still it is not what you really need. 这将起作用,但仍然不是您真正需要的。 Really, you operate with strings but then transform string to byte array manually and then read bytes in order to manually transform them to String again? 确实,您使用字符串进行操作,但是随后将字符串手动转换为字节数组,然后读取字节以再次将它们手动转换为String?

Instead you should use PrintWriter to write strings: 相反,您应该使用PrintWriter编写字符串:

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

and then use BufferedReader when you are reading the data. 然后在读取数据时使用BufferedReader。

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

If you want to send bytes, I suggest you read bytes, If you want to send text I suggest you read text. 如果要发送字节,我建议您阅读字节,如果要发送文本,我建议您阅读文本。 You shouldn't try to mix and match or you are likely to get yourself confused. 您不应该尝试混合搭配,否则可能会感到困惑。

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();

prints 版画

70798090999a9b9c9d9e9fa0a1a2

Get rid of all the ready() tests. 摆脱所有ready()测试。 The reads will block until data arrives, so calling ready() is at heat literally a waste of time, and risks missing data. 读取将阻塞,直到数据到达为止,因此调用ready()确实浪费时间,并且有丢失数据的风险。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM