user1673627
user1673627

Reputation: 271

Stream Write Exception

I am doing socket programming. I have two files called Server.java and client.java. Both programs were running successfully and successfully sending data to each other. Yesterday I added the following lines to client.java file:

     //old line
     out.writeUTF(dbdata); //dbdata was written successfully and sent to the server program
     try
     {
          //my socket connection object name is sock
            String data1="1000";
            System.out.println(data1);
            out.writeUTF(data1);   this line causes the error
     }

     catch(Exception e)
     {
            System.out.println("Exception :: "+e);
     }

When the line out.writeUTF(data1) is executed and catch catches it and shows the exception as BROKEN PIPE.

The contents of server.java which reads data1 is given below:

     String data1;
     try
     {
            data1=in.readUTF();
     }
     catch(Exception e)
     {
            System.out.println("Server Exception :: "+e);
     }

I also checked if connection is open with isConnected() before out.writeUTF(data1) and after the exception occurs in the catch I executed isConnected(). Both the times it showed True only.

Upvotes: 0

Views: 224

Answers (2)

Stephen C
Stephen C

Reputation: 719576

@EJP is right. The exception means that the socket was closed before the write call could complete, and possibly before it started. There is no way of knowing how much of the data was delivered to the other end.

The other part of the puzzle is why isConnected() is returning true. That is explained by the javadoc which says:

"Note: Closing a socket doesn't clear its connection state, which means this method will return true for a closed socket (see isClosed()) if it was successfuly connected prior to being closed."

Upvotes: 1

user207421
user207421

Reputation: 311050

'Broken pipe' means you have written to a connection that has already been closed by the other end. In other words, an application protocol error. The mitigation is not to do it, either by fixing this end so it doesn't write unwanted data, or fixing the other end so it reads all the wanted data.

Upvotes: 1

Related Questions