客户端:
out = new DataOutputStream(connection.getOutputStream());
String process;
System.out.println("Connecting to server on "+ host + " port " + port +" at " + timestamp);
process = "Connection: "+host + ","+port+","+timestamp;
System.out.println("client len " + process.length());
out.write(process.length());
打印: 客户端长度 55
服务器端:
in = new DataInputStream(connection.getInputStream());
while (true) {
int len = in.readInt();
System.out.println("Length of pkt: "+len);
打印:pkt 的长度:927166318
这是怎么回事?我试着写 0,它在服务器端打印了 3621743。我检查了其他一些网站,一些人在使用其他流时遇到了问题。我读到了大字节序与小字节序引起的问题,但我不确定这里的问题是什么,因为我使用的数据*流应该可以很好地相互配合。
最佳答案
如果你在一侧调用readInt()
,你应该调用writeInt(int)
在另一。改变这个
out.write(process.length());
到
out.writeInt(process.length());
来自 write(int)
的 Javadoc ,
Writes the specified byte (the low eight bits of the argument b) to the underlying output stream.
关于Java数据输入流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26308941/