0

I tried to send an int value from the client to the server. This is the client code that I'm using below:

_port   = 8071;
_socket = new Socket("localhost", _port);

Random rand = new Random();
int  n = rand.nextInt(50) + 1;
DataOutputStream dos = new DataOutputStream(_socket.getOutputStream());
dos.writeInt(n);
dos.flush();

Server Code

try {
    input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    ObjectInputStream in = null;
    in = new ObjectInputStream(socket.getInputStream());
    int ClientNumber= in.readInt();
    System.out.println(ClientNumber);
}

but I am getting an invalid stream header error.

invalid stream header: 0000002B at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:781) at java.io.ObjectInputStream.(ObjectInputStream.java:278) at ServiceRequest.run(ServiceRequest.java:24) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) at java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) at java.lang.Thread.run(Thread.java:680)

Does anyone know what is causing the error? Is my code setup improperly?

1
  • Why are you creating an InputStreamReader, wrapping it in a BufferedReader`, and then completely ignoring it? Commented Apr 30, 2013 at 16:17

2 Answers 2

1

You're writing using a DataOutputStream and reading using an ObjectInputStream. You should be using DataInputStream instead:

// Note declaration and assignment in a single statement. There's no point in
// making it null first.
DataInputStream in = new DataInputStream(socket.getInputStream());

// Note use of camelCase for variable name
int clientNumber = in.readInt();

You should also get rid of input here: you're not reading from it, and as this looks like it's a stream of binary data, it's inappropriate to treat it as text.

Oh, and you should be closing the input stream in a finally block.

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

Comments

0

try to change it like this

try {
             //   input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            DataInputStream in = new DataInputStream(socket.getInputStream());
            int clientNumber= in.readInt();
            System.out.println(clientNumber);
}

it should work. ObjectInputStream can read only streams sent by ObjectOuputStream, it starts with a magic number (header) 0xACED, see http://docs.oracle.com/javase/6/docs/platform/serialization/spec/protocol.html

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.