0

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?

n1ckolas
  • 4,380
  • 3
  • 37
  • 43

1 Answers1

1

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.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264