如何使用套接字拒绝c#中的连接尝试
问题描述:
我正在开发ac#windows表单应用程序,我有一个服务器和多客户端,但客户端数量必须限制,所以如何在这种情况下拒绝连接?
I'm developping a c# windows form app , I have a server and multiclients but the number of clients must be limited so how can I reject a connection in this case ??
答
在服务器端,每次客户端连接时,都会获得一个新的套接字。跟踪号码连接(使用计数器,列表或其他)。当客户端断开连接然后递减计数器时,从列表中删除等。当你达到所需的阈值
然后通过关闭套接字来删除后续连接尝试。
On the server side, each time a client connects you'll get a new socket. Keep track of the number connects (using a counter, list or whatever). When the client disconnects then decrement the counter, remove from the list, etc. When you get to the threshold you desire then drop subsequent connection attempts by closing the socket.