dpathakj / junixsocket

Automatically exported from code.google.com/p/junixsocket
0 stars 0 forks source link

IOException when using BufferedReader.readLine() #9

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create a unix domain socket (as a Server application)
2. Read data from unix domain socket with BufferedReader.readLine() 
3. Client side closes connection
4. IOException will occur 'Underlying input stream returned zero bytes'

What is the expected output? What do you see instead?
No IOException when client closes connection. Data is transferred successful 
but close is not 
handled correct. Should behave like TCP/IP socket.

What version of the product are you using? On what operating system?
Version 1.1 on Mac OS X 10.6 (java version "1.6.0_17"), problem also exists on 
Debian.

Please provide any additional information below.
I am using the same code with a TCP/IP socket and it works great but with 
junixsocket i am 
getting an IOException when the connection is closed on the client side. It is 
not a big issue 
because the data transfers is correct. On the client side I am using PHP with 
fread and 
fsockopen. Based on the information I did find on this exception it has 
something to do with the 
-1 status code.

Example Java code:
// Read while no more data is send aka null
while( (inputLine = in.readLine()) != null){
    // Parse input based on communication protocol and send result to client
    result = parseResult(inputLine);
    out.write(result[0]);
    out.flush();

    // Close connection if requested
    if(result[1]){
        socket.close();
    }
}

Stack trace
sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:268)
sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
java.io.InputStreamReader.read(InputStreamReader.java:167)
java.io.BufferedReader.fill(BufferedReader.java:136)
java.io.BufferedReader.readLine(BufferedReader.java:299)
java.io.BufferedReader.readLine(BufferedReader.java:362)
myproject.ServerThread.run(ServerThread.java:54)

"java.io.IOException: Underlying input stream returned zero bytes"

Would be great if you could look in to this problem and thanks for this great 
project!

Original issue reported on code.google.com by vincentheet on 8 Mar 2010 at 10:00

GoogleCodeExporter commented 9 years ago
Patch attached.  read(2) returns a 0 on EOF and java returns a -1.  A -1 in 
read(2)
is indicative of an error.

It is not acceptable to return 0 bytes in normal Sockets.  Java nio channels 
support
this.  I removed the code which handled the EWOULDBLOCK or EAGAIN errors since
non-blocking is not supported.

Original comment by derrick....@gmail.com on 7 Apr 2010 at 5:51

Attachments:

GoogleCodeExporter commented 9 years ago
Test cases attached.  Requires junit4.

Original comment by derrick....@gmail.com on 7 Apr 2010 at 5:52

Attachments:

GoogleCodeExporter commented 9 years ago
Nice catch!
Junit tests look sweet. Compiles, works. Committed to SVN.

Thanks vincentheet, thanks Derrick!

Original comment by ckkohl79 on 7 Apr 2010 at 9:02