[英]Read data from a client socket seems miss bytes (from java to python)
I have recently just started to learn networking and google's protocol buffers to build a communication between my java client to a python server. 我最近才刚刚开始学习网络和Google的协议缓冲区,以在Java客户端与python服务器之间建立通信。 Sending message from my python server to java client works perfect, but the reverse way (from java to python) was always failed.
从我的python服务器向Java客户端发送消息的过程完美,但是相反的方式(从Java到python)始终失败。
After checking the length of message from both sides, I have found the receiver(python) missing some of the bytes transferred by the java code, 在从双方检查了消息的长度之后,我发现接收器(python)缺少了Java代码传输的某些字节,
java side:
Java端:
12 28 08 0b 12 24 15 00 00 80 3f 1a 1b 09 00 00 00 00 00 00 f0 3f 11 00 00 00 00 00 00 f0 3f 19 00 00 00 00 00 00 f0 3f 22 0012 28 08 0b 12 24 15 00 00 80 3f 1a 1b 09 00 00 00 00 00 00 f0 3f 11 00 00 00 00 00 00 00 00 f0 3f 19 00 00 00 00 00 00 00 f0 3f 22 00
python side:python端:
12 28 08 0b 12 24 15 00 00 80 3f 1a 1b 09 00 00 00 00 00 00 f0 3f 11 00 00 00 00 00 00 f0 3f12 28 08 0b 12 24 15 00 00 80 3f 1a 1b 09 00 00 00 00 00 00 f0 3f 11 00 00 00 00 00 00 00 f0 3f
As you can see, the last 10 bytes is missing. 如您所见,最后10个字节丢失了。 I could not figure out the problem.
我不知道问题所在。
The following is my java code, 以下是我的java代码,
// Both Message and UPDATEs are message types I defined in my .proto file
Message message = Message.newBuilder()
.setUpdate(UPDATEs.newBuilder()
.setTimeStamp(11)
.addUpdates(state))
.build();
//System.out.println(message.toString());
System.out.println(message.toByteArray().length); // result -> 42
//System.out.println(byteArrayToHex(message.toByteArray()));
try {
OutputStream outputStream = socket.getOutputStream();
message.writeDelimitedTo(outputStream);
System.out.println("Sending finished.");
} catch (IOException e) {
System.err.println("SteerCommunicator_sendCarData(OutputStream output): " + e.toString());
System.exit(0);
}
and my python code, 还有我的python代码
def server_loop():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "Listening at port {}".format(PORT)
try:
server.bind(("localhost", PORT))
except:
print "Unable to listen on the Port" + PORT
sys.exit()
server.listen(10)
while True:
client_socket, addr = server.accept()
print "Received Message from Client {}:{}".format(addr[0],str(addr[1]))
msg = client_socket.recv(1024)
print len(msg) # result -> 32
Any ideas? 有任何想法吗?
If the diagnosis that the stream is not being flushed is correct, then here is the solution: 如果诊断为未冲洗流是正确的,则以下为解决方法:
try (OutputStream outputStream = socket.getOutputStream()) {
message.writeDelimitedTo(outputStream);
System.out.println("Sending finished.");
} catch (IOException e) {
// SEE NOTES!!
System.err.println("blah blah" + e.toString());
System.exit(0);
}
Explanation: by using try-with-resources , we ensure that the resource is always closed as the block exits, no matter how it exits. 说明:通过使用try-with-resources ,我们确保无论块如何退出,资源总是在块退出时关闭。 This has two benefits:
这有两个好处:
FileDescriptor
). FileDescriptor
)。 Resource leaks can cause various unrelated I/O activity to fail ... later on ... if the GC doesn't get a chance to clean up the mess first. NOTES: 笔记:
Calling System.exit(...)
in the depths of your code is a bad idea. 在代码的深处调用
System.exit(...)
是一个坏主意。 It preempts other possible approaches to recovery, and makes your code harder to extend and/or reuse. 它优先于其他可能的恢复方法,并使您的代码难以扩展和/或重用。
If you catch and report an exception, you should also report the stacktrace ... somewhere. 如果捕获并报告异常,则还应该在某处报告stacktrace...。
A better strategy would be to declare IOException
as thrown by the enclosing method, and catch / report / recover at a higher level. 更好的策略是将
IOException
声明为由封装方法抛出,并在更高级别捕获/报告/恢复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.