简体   繁体   中英

Java Socket - Broken pipe error

I am trying to send int value, long value, long array and 2d double array via socket from the Client to the Server.

I successfully sent int, long values and long array, however when it comes to the double array (output.writeObject(server_ind); - see Client Side code below), I am getting the following error:

ERROR:

java.net.SocketException: Broken pipe
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:159)
    at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1876)
    at java.io.ObjectOutputStream$BlockDataOutputStream.writeByte(ObjectOutputStream.java:1914)
    at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1575)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:350)
    at clientSide.ClientSocket.connectionProtocol(ClientSocket.java:36)
    at clientSide.clientMain.main(clientMain.java:97)

My code is the following:

Client Side:

        ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());

        output.writeObject(num_doc); //int value
        output.flush();

        output.writeObject(num); //long value
        output.flush();

        output.writeObject(queryTDs); //long[] array
        output.flush();

        output.writeObject(server_ind); //double[][]
        output.flush();

Server Side:

    input = new ObjectInputStream(clientSocket.getInputStream());

    num_doc = input.readInt();
    num = input.readLong();
    TDs = (long[]) input.readObject();
    server_ind = (double[][]) input.readObject();

    output = new ObjectOutputStream(clientSocket.getOutputStream());
    output.writeObject("Received");

Thanks!

The Broken pipe Exception happen when the connection between sender and receiver is closed by reciver (in this case the Server Side) before the sender finish to send all the stream.

Checking your code i notice that:

In the first case, you are sending and object of 32 bits ( int ):

output.writeObject(num_doc); //int value output.flush();

And wait for 32 bits:

num_doc = input.readInt();

The same for the second case, when you send and recived 64 bits ( long type)

In the case of an long[] you are sending an Object wich its size depends of the amount and type of the data, according whith oracle documentation, for `ObjectOutputStream:

Write the specified object to the ObjectOutputStream. The class of the object, the signature of the class, and the values of the non-transient and non-static fields of the class and all of its supertypes are written

But in the case of double[][] , you are sending an ( double[] ) that each element has another double[] . For some reason, that is not clear for me, the ObjectInputStream is not able to read all the objects that had been sended by your client.

So, it could be that the receiver doesn't know how many byte need to read to build the double array.

One question for you (@Liutauras94): Is there a exception in the server side?

I recomend you to convert the double value to an array of 4 bytes (64 bits) and send it with a method write(byte[]) instead of writeObject. Be aweare of the order of byte that recive the other part.

Also, in Java, there are another kind of stream writers that handle numeric data types, they are better than try to send an object.

You're writing objects but reading primitives. So eventually you read less data than is really there, not to mention entirely wrong data; then you close the socket while there is still unread pending data in it; and that causes a connection reset to be propagated to the writer; which causes the 'broken pipe'.

If you write data with writeObject() you must read it with readObject() . If you write data with writeInt() you must read it with readInt() . And so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM