0

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. 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.

I can only see the data at the server when i try to send 500K + bytes. 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/

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 ?

4

2 回答 2

1

在您的代码示例中, PrintWriter 设置为自动刷新。这意味着当println()被调用时,缓冲区将被刷新,并因此被发送到网络。但是,您使用 BufferedOutputStream 并且可能忘记了flush()您的 OutputStream。

于 2013-09-20T13:29:35.943 回答
0

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

于 2013-09-20T13:42:26.943 回答