简体   繁体   English

Python Java套接字问题

[英]Python Java Socket Issues

I am having an issue with Java ServerSocket and Python where I have an multithreaded echo server (Written in Java) that can communicate with multiple java clients. 我在Java ServerSocket和Python上遇到问题,我在其中有一个可以与多个Java客户端通信的多线程回显服务器(用Java编写)。 This all works fine. 这一切都很好。

When I try to connect with a python client, the python client can receive data from the server but when it sends data the server appears never to receive it. 当我尝试与python客户端连接时,python客户端可以从服务器接收数据,但是当它发送数据时,服务器似乎永远不会接收到它。

I can only see the data at the server when i try to send 500K + bytes. 当我尝试发送500K +字节时,我只能在服务器上看到数据。 While i can now see the data its is incomplete and out of sync. 现在,我可以看到数据不完整且不同步了。

I tested the follwing example code and it works fine with python: http://norwied.wordpress.com/2012/04/17/how-to-connect-a-python-client-to-java-server-with-tcp-sockets/ 我测试了下面的示例代码,它在python上运行良好: http : //norwied.wordpress.com/2012/04/17/how-to-connect-a-python-client-to-java-server-with-tcp -插座/

The only real difference is that in the code from the link it uses: 唯一真正的区别是,它使用链接的代码:

BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out = new PrintWriter(client.getOutputStream(),

Where as in my server I use: 我在服务器上使用的位置:

streamOut = new DataOutputStream(new BufferedOutputStream(s.getOutputStream())); 
streamIn = new DataInputStream(new BufferedInputStream(socket.getInputStream()));

Could this be causing the issue ? 这可能是造成问题的原因吗?

In your code example, the PrintWriter was set to autoflush. 在您的代码示例中,PrintWriter设置为自动刷新。 This means that when println() is called, the buffer will be flushed and, consequently, sent to the network. 这意味着当调用println()时,缓冲区将被刷新并因此发送到网络。 You however, use a BufferedOutputStream and probably forgot to flush() your OutputStream. 但是,您使用BufferedOutputStream并可能忘记了flush() OutputStream。

我认为您的python客户端没有刷新流,这就是Java服务器仅在客户端缓冲区已满后才获取数据的原因。

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

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