这是一个基于 UDP 协议与所有客户端通信的 C++ 服务器应用程序。当用户从客户端登录到服务器时,客户端应用程序向服务器注册一个UDP通道,该通道的格式是固定的:IP+Port,即如果IP保持不变,那么无论客户端登录的用户注册什么同一个频道。
服务器的socket层维护了一个心跳机制,如果3分钟内没有收到来自通道的心跳包,就会移除通道。一切正常,直到客户端关闭,例如网络线被拔掉。看下面的场景:
1. User-A logs into server. The Client registers channel (IP:Port)
to the server. Because the UDP channel is alive, so the Server
sets the User status of User-A as Online.
2. Kill the client process, and within 3 minutes(before the channel
timeouts in server), let User-B logs into server from the same
computer. Because the IP remains unchanged, so actually the client
registers a same (IP:PORT) pair to the server as it did when User-A
logs in.
3. Since the Server receives packets from (IP:PORT), so it considers
User-A is still alive, thus setting the user status of User-A as
Online which is not right anymore.
在上述情况下,服务器无法区分从同一台计算机登录的不同用户,从而导致错误的用户状态。有谁知道如何解决这个问题?