0

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!

4
  • could you show the meaningful code? The exception is in: clientSide.ClientSocket.connectionProtocol(ClientSocket.java:36) But is not in the post Commented Feb 8, 2017 at 14:01
  • As per description above - "however when it comes to the double array (output.writeObject(server_ind); - see Client Side code below)", so output.writeObject(server_ind); thats the 36th line, which is in the code provided, apologise if its not clear enough. Commented Feb 8, 2017 at 14:09
  • What is in server logs? I suppose somewhere near line TDs = (long[]) input.readObject(); your code throw exception. Commented Feb 8, 2017 at 14:14
  • I would debiug both sides and you will get the cause of the exception. It easier,faster than all the typing and formatting and waiting for answers Commented Feb 8, 2017 at 14:41

3 Answers 3

2

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?

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer, that was exactly my issue. I was sending an object but expecting and int. :)
0

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.

3 Comments

You recommend it why?
Because it easier make a low level debug to know what information is sended by socket, with the stream of data you can print and analyze the order of bytes that is sended, also you can create a log which has the raw data in hex format of information that is recived o sended, this information is usefull when you are creating server and clients over difereten platforms, like Java and .Net. That is the reason that i recommend it.
So all that information should appear in your answer, instead of having to be asked for, but there is nothing there that identifies why the OP got a 'broken pipe' error. And this is not a multi-platform question: it is about Java Serialization.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.