Reputation: 1
I have the following code:
public class StartSocket{
serverSocket = new ServerSocket(listenPort);
while (listening)
{
new ServerThread(serverSocket.accept()).start();
}
}
The ServerThread
is used to communicate with the client. It sends heartbeat messages every 4 seconds. When I have bandwidth issues the client attempts to reestablish the connection. As such the ServerThread
is continually being opened. This causes an overload on the server.
How do I manage the re-connection attempts by the client?
Upvotes: 0
Views: 49
Reputation: 114588
Have the server identify the client in ServerThread
. Have a map of client identifiers to server threads (on the server). If a client with the same identifier connects, close the old server connection to that client. This will guarantee that the client can really reconnect if the heartbeat dies for a legitimate reason, and that the server will not have more than one connection per client.
Upvotes: 1