简体   繁体   English

服务器套接字重新连接问题

[英]Server Socket Re-connection Issue

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. ServerThread用于与客户端通信。 It sends heartbeat messages every 4 seconds. 它每4秒发送一次心跳消息。 When I have bandwidth issues the client attempts to reestablish the connection. 当我遇到带宽问题时,客户端会尝试重新建立连接。 As such the ServerThread is continually being opened. 这样, ServerThread会不断打开。 This causes an overload on the server. 这会导致服务器过载。

How do I manage the re-connection attempts by the client? 如何管理客户端的重新连接尝试?

Have the server identify the client in ServerThread . 让服务器在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. 这将确保如果心跳由于正当原因而中断,则客户端可以真正重新连接,并且服务器对每个客户端的连接不会超过一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM