Java Socket fails to transmit data over the network

297 Views Asked by At

I'm experiencing a bizarre problem with sockets between a Java Knopflerfish client bundle and a PHP (CLI, not web) server.

The client/server pair work fine when both are located on the localhost, and all data is transmitted successfully. However, when the Java client exists on a different machine, connections to the server are successful, but no data is received by the PHP script. Packet analysis confirms that the data sent by the Java client is received on by the server - PHP just seems to have problems getting its hands on it.

As a further note, I've done some tests with telnet as the client. The PHP server script receives all data fine from any host. This leads me to believe that the problem has something to do with the way java is setting up the socket or that there is some networking issue that I'm not familiar with. Any thoughts would be appreciated. Can post code samples if desired.

2

There are 2 best solutions below

2
On

You probably forgot to flush the buffers on the client side. telnet does a flush for every line, so that would explain why it works.

0
On

Turns out it was a race condition. If I sleep for a few milliseconds after receiving a connection and before doing a socket_read, it does the trick nicely.