Vincent Verdot

Greenhorn
+ Follow
since Mar 30, 2008
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Vincent Verdot

Originally posted by Ernest Friedman-Hill:
although here I think all you really wanted was the readLine() method anyway, right?



Yes, it's true . Anyway, I am rather satisfied of my second solution (don't know if you saw it -- cross-posted ? --). I don't know if it is efficient but it works fine.
You helped me when you told that I cannot (at least I should not) use a InputStream and a BufferedReader (applied to the same input) concurrently. I wasn't sure about that because I didn't find any information in javadocs and other forums.. Thanks.
13 years ago
Here my solution (if it can help).
The client side is almost the same, except that I no more use PrintWriter.println(). Instead I print the String and concatenate '\n', thus I don't have to handle "\r\n" on server side.

BTW, I am still wondering why can I use an InputStream and a BufferedReader from a same stream in some situations (e.g. when client and server are on different hosts).

Here, I use a BufferedInputStream for efficiency (compared to InputStream) but I have to parse each String manually. I think this solution is suitable if the mainly transmitted information is raw data.



13 years ago

Originally posted by Ernest Friedman-Hill:
you should never read directly from that stream again.



That's ok, so in my case how could I get bytes from the BufferedReader?
13 years ago
Hello all,
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.



13 years ago