Getting timeout on validation, while debugging with JMeter keep getting exception in Connection constructor while calling socket.getInputStream():
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2681)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3156)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:862)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:358)
at com.codegym.task.task30.task3008.Connection.<init>(Connection.java:16)
at com.codegym.task.task30.task3008.Server$Handler.run(Server.java:25)
What am I doing wrong?package com.codegym.task.task30.task3008;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Server {
private static Map<String, Connection> connectionMap = new ConcurrentHashMap<>();
private static class Handler extends Thread {
private Socket socket;
public Handler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
String userName = null;
Connection connection = null;
try {
connection = new Connection(socket);
ConsoleHelper.writeMessage("Connection established with remote address: " + connection.getRemoteSocketAddress());
userName = serverHandshake(connection);
sendBroadcastMessage(new Message(MessageType.USER_ADDED, userName));
notifyUsers(connection, userName);
//serverMainLoop(connection, userName);
} catch (IOException e) {
ConsoleHelper.writeMessage("Error occured while communicating with remote address");
if (connection != null) {
try {
connection.close();
} catch (IOException ec) {}
}
if (userName != null) {
connectionMap.remove(userName);
sendBroadcastMessage(new Message(MessageType.USER_REMOVED, userName));
}
ConsoleHelper.writeMessage("Connection with the remote address is closed.");
} //catch (ClassNotFoundException e) {
// ConsoleHelper.writeMessage("Error occured while communicating with remote address");
// }
}
private String serverHandshake(Connection connection) throws IOException {
Message message = new Message(MessageType.NAME_REQUEST);
Message receivedMessage = null;
do {
connection.send(message);
try {
receivedMessage = connection.receive();
} catch (ClassNotFoundException e) {
}
} while (receivedMessage == null ||
!receivedMessage.getType().equals(MessageType.USER_NAME) ||
receivedMessage.getData() == null ||
receivedMessage.getData().isEmpty() ||
connectionMap.containsKey(receivedMessage.getData())
);
connectionMap.put(receivedMessage.getData(), connection);
connection.send(new Message(MessageType.NAME_ACCEPTED));
return receivedMessage.getData();
}
private void notifyUsers(Connection connection, String userName) throws IOException {
connectionMap.forEach((u, c) -> {
if (!u.equals(userName)) {
try {
connection.send(new Message(MessageType.USER_ADDED, u));
} catch (IOException e) {
ConsoleHelper.writeMessage("Can't send message to '" + userName + "'");
e.printStackTrace();
}
}
});
}
private void serverMainLoop(Connection connection, String userName) throws IOException, ClassNotFoundException {
while (true) {
try {
Message received = connection.receive();
if (received.getType().equals(MessageType.TEXT)) {
String m = userName + ": " + received.getData();
sendBroadcastMessage(new Message(MessageType.TEXT, m));
} else {
String error = "not text error";
ConsoleHelper.writeMessage(error);
}
} catch (Exception e) {
ConsoleHelper.writeMessage(e.getMessage());
}
}
}
}
public static void main(String[] args) {
ConsoleHelper.writeMessage("Please, enter server port:");
int port = ConsoleHelper.readInt();
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Server is listening on port " + port);
try {
Socket socket;
while ((socket = serverSocket.accept()) != null) {
new Handler(socket).start();
}
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} finally {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void sendBroadcastMessage(Message message) {
for (Map.Entry<String, Connection> connectionEntry : connectionMap.entrySet()) {
try {
connectionEntry.getValue().send(message);
} catch (IOException e) {
System.out.println("Can't send mesage to user '" + connectionEntry.getKey() + "'");
e.printStackTrace();
}
}
}
}