Reputation: 1100
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 ?
Upvotes: 0
Views: 346
Reputation: 2812
I think your python client isn't flushing the stream, which is why java server only gets data after the buffer on the client is full.
Upvotes: 0
Reputation: 8163
In your code example, the PrintWriter was set to autoflush. This means that when println()
is called, the buffer will be flushed and, consequently, sent to the network. You however, use a BufferedOutputStream and probably forgot to flush()
your OutputStream.
Upvotes: 1