I am writing a program composed of a server and a client (see code below). The client must send data (byte) to the server.. To achieve this, it sends a name (identifying the data), a size (also as String), and finally the data.
On client side I create a socket and write the name/size via a PrintWriter and the raw data directly via a OutputStream.
On server side, I read the name and the size via a BufferedReader and then the raw data via the InputStream.
The problem is that when reading the data, input.read() always returns -1 (end of stream I guess). (of course, name and size are correctly transmitted).
I printed some debug information (input.available()) and the InputStream seems to have all its data consumed by the BufferedReader.
I already found another way to achieve the transfer succesfully (e.g. by using only BufferedReader) but I don't find the method convenient and I don't understand why I cannot use InputStream and BufferedReader. Actually I am looking for an explanation.
Last but not least, I am using this code (client and server) in another part of my program (exactly the same code) and guess what ? It works perfectly
. The only difference I found is that my problem occurs when the client and the server are on the same machine (IPC purpose). In other words, the same code works correctly when client and server are on different machines. (Maybe the BufferedReader reads more data in local conditions?).
Any idea? Thank you in advance.