2

I am writing a socket client in which I am sending data to server (using getOutputStream()),below is my code

 this.wr = this.socket.getOutputStream();

  wr.write(hexStringToByteArray(messageBody));

wr.flush(); 

The above is successfull able to send the data. 1) but when I try to read the response using

this.in = new ObjectInputStream(this.socket.getInputStream());

As I dont know what format the server is returning. Getting error at this line

"java.io.StreamCorruptedException: invalid stream header" .

I am not sure why ? I know the values that I will recieve will be in the hex format i.e say 600185 would be as in 60 01 86 ....

Could any one please help me, to over come this error.

2) Also in case if I dont receive any response after certain duration, how to close the socket connection.

Thanking you all in advance.

1 Answer 1

5

ObjectInputStream expects a header in the stream that is written by ObjectOutputStream. So If you use one, you need to use both.

As your sample doesn't really need ObjectOutputStream, you may just want to not use ObjectInputStream.

something like:

public void doWrite(Socket socket, String messageBody) {
    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
    byte[] data = hexStringToByteArray(messageBody);

    dos.writeInt(data.length);
    dos.write(data);
    dos.flush();
}

public String doRead(Socket socket) throws IOException {
    DataInputStream dis = new DataInputStream(socket.getInputStream());
    int len = dis.readInt();
    byte[] data = new byte[len];

    dis.read(data);

    return byteArrayToHexString(data);
}
Sign up to request clarification or add additional context in comments.

5 Comments

So how will I read the Response from the Server, any suggestion. I just tried using this.in = this.socket.getInputStream();, System.out.println(this.in.read()); but ot getting the desired result.
you are writing a byte array. so when reading you probably need to know how big of an array to read, so I'd write the length of the array first, then the array. On read, read the length, then the array. To write integers to a stream, wrap the stream with a DataOutputStream, and on read with a DataInputStream. you can use DataOutputStream.writeInt and DataInputStream.readInt. To read the array, do read(array); after you allocate the array based on the previously read size.
Thank you, but is there any way to know dynamically the size that I want to read as I am not sure what size the server may response. Also any idea if I dont receive any response the socket connection should close to avoid lots of open connection
I tried using the code that you gave , I am not sure where i went wrong but my eclipse got hanged & checked the process was ongoing. So I had to terminate it. I tried with InputStream I am getting the required result as Expected but, I need to initialize the byte array depending upon the Server size, here I am failing for the same as i dont know how much the size would be
@MeByFatGuy, thanks for the sample code. Helped me in the same problem. I did not read the length first and that is why I had that error. +1 !

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.