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?
InputStreamReader, wrapping it in aBufferedReader`, and then completely ignoring it?