35

I am trying to read a string which is send from client using Socket program, The code as follows:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.ClassNotFoundException;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketServerExample {

    //static ServerSocket variable
    private static ServerSocket server;
    //socket server port on which it will listen
    private static int port = 5000;

    public static void main(String args[]) throws IOException, ClassNotFoundException{
        //create the socket server object
        server = new ServerSocket(port);
        //keep listens indefinitely until receives 'exit' call or program terminates
        while(true){
            System.out.println("Waiting for client request");
            //creating socket and waiting for client connection
            Socket socket = server.accept();
            //read from socket to ObjectInputStream object
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            //convert ObjectInputStream object to String
            String message = (String) ois.readObject();
            System.out.println("Message Received: " + message);
            //create ObjectOutputStream object
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            //write object to Socket
            oos.writeObject("Hi Client "+message);
            //close resources
            ois.close();
            oos.close();
            socket.close();
            //terminate the server if client sends exit request
            if(message.equalsIgnoreCase("exit")) break;
        }
        System.out.println("Shutting down Socket server!!");
        //close the ServerSocket object
        server.close();
    }

}

But I am getting error as follows while reading the string from client:

Exception in thread "main" java.io.StreamCorruptedException: invalid stream header: 54657374
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:803)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:298)
    at SocketServerExample.main(SocketServerExample.java:29)

I searched and not managed find the bug. Please help me.

androidGenX
  • 1,108
  • 3
  • 18
  • 44
  • 1
    maybe duplicate of http://stackoverflow.com/questions/2622716/java-invalid-stream-header-problem – richard Apr 24 '14 at 07:14

2 Answers2

50

Clearly you aren't sending the data with ObjectOutputStream: you are just writing the bytes.

  • If you read with readObject() you must write with writeObject().
  • If you read with readUTF() you must write with writeUTF().
  • If you read with readXXX() you must write with writeXXX(), for most values of XXX.
user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    @androidsuckzzz To read on an ObjectInputStream, you have to write to an ObjectOutputStream (or you have to do the same thing it does which I don't suggest you try). You can't just write to any stream and assume it will work. – Peter Lawrey Apr 24 '14 at 07:48
  • 1
    @SrujanBarai No you aren't. You are sending `"Test"` directly, or else you aren't getting this error. – user207421 Nov 21 '18 at 23:17
9

You can't expect ObjectInputStream to automagically convert text into objects. The hexadecimal 54657374 is "Test" as text. You must be sending it directly as bytes.

user207421
  • 305,947
  • 44
  • 307
  • 483
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Hello, I am sending bytes directly from my cpp client to java server. How to read it in this case? iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 ); -- cpp client code for sending. – Arka Mallick Feb 25 '19 at 00:22
  • 1
    @Haramoz You should also send the length otherwise you can only send one message per stream. I would use blocking NIO with ByteBuffer with native ordered data. – Peter Lawrey Feb 27 '19 at 15:12