I am using Socket
to send a task from one server to another like so:
private boolean sendRequest(String address, int port) {
boolean requestComplete = false;
try {
Socket socket = new Socket(address, port);
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject("task_to_complete");
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
String msg = (String)ois.readObject();
if(msg.equals("complete")){
requestComplete = true;
}
ois.close();
oos.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
return requestComplete;
}
The second server receives the task like so:
while (true) {
// wait for connection
Socket socket = serverSocket.accept();
System.out.println("New connection accepted " + socket.getInetAddress() + ":" + socket.getPort());
// retrieve request from server
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
String msg = (String) ois.readObject();
switch (msg) {
case "task_to_complete":
// do task 1
break;
}
System.out.println("Task " + msg + " complete.");
// send a message back to client with the result of the task it
// requested
PrintWriter out =
new PrintWriter(socket.getOutputStream(), true);
out.print("complete");
ois.close();
socket.close();
}
But I get the error
java.io.EOFException at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
when I attempt to read the message "complete" from the first server:
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
.
What is causing this error?