服务器如何知道客户端仍然连接?
客户端与服务器进程建立连接。服务器如何知道客户端仍然连接?
例如,服务器是网络服务器,客户端是浏览器。我们假设要求是服务器应该始终想知道客户端已连接。
它是如何实现/设计的?
a client establishes a connection to a server process. how does the server knows that the client is still connected?
For example the server is a web server and a client is a browser. Let's assume that the requirement is that the server should always wants to know that the client is connected.
How is that implemented/designed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HTTP 会话可以提供帮助。
HTTP Sessions can help.
我假设你在谈论套接字。
服务器会知道客户端仍然处于连接状态,因为当客户端断开连接时,连接将被断开。作为 TCP 正常工作的一部分,服务器将收到此通知,因此关联的套接字将被标记为不再与连接关联。
当服务器进程从此套接字读取或写入时,他们将被告知连接不再存在。
I assume you're talking about sockets.
The server will know that the client is still connected because when the client disconnect, the connection will be torn down. The server will be notified of this as part of the normal workings of TCP, and thus the associated socket will be marked as no longer being associated with a connection.
When the server process goes to read or write from this socket, they'll be informed that the connection no longer exists.
假设正在使用 TCP,当另一端断开连接时,您将在套接字上收到零字节读取。这表示连接已关闭。
如果服务器在连接关闭时向套接字写入数据,它也会收到连接已关闭的通知,但具体如何给出该通知在不同的实现中是不同的。
Assuming TCP is being used, you will receive a zero byte read on the socket when the other side has disconnected. This signifies that the connection is closed.
If the server is writing data to a socket while the connection is closed, it will also receive notification that the connection has been closed, but exactly how this notification is given is different across implementations.