了解插座中的断开连接 [英] Understand disconnect in sockets

查看:111
本文介绍了了解插座中的断开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我正在创建一个聊天应用程序.当用户断开连接(应用程序的最终处理过程中,正在断开与网络的连接...)时,服务器中的Socket为Connected = True.当用户不与服务器断开连接而无法断开连接时,我怎么无法理解?(例如yahoo Messenger.当用户与其他朋友断开连接时,就会看到).

Hello
I''m creating a chat application. When a user disconnects(with end proccess of app, disconnecting from network ,...) the Socket in the server is Connected=True. How i cant understand when a user disconnects without saying to server?(like yahoo messenger. when a user disconects other friends will see).

推荐答案

Socket.Connected表示上次读取或写入操作时套接字的状态,而不是套接字的当前状态.有几种不同的方法来处理断开连接:

1)当客户端断开连接时,它可以向服务器发送一条消息,告知它即将断开连接.
2)让服务器使用 Socket.Poll [ ^ ]获取当前状态的方法.从中返回的值可以与其他信息组合以确定套接字是否仍处于打开状态.
3)让服务器定期向客户端发送心跳消息以测试连接.如果关闭,则发送将失败.

使用(1)会在客户端断开连接时立即通知您,而(3)将处理不正确断开连接的客户端. (3)还将使客户端知道套接字状态,因为它可以检查心跳. (2)与(3)相似,只是没有任何内容发送到客户端,因此客户端和服务器都必须主动检查套接字状态.
Socket.Connected represents the state of the socket at the last read or write operation, not the current state of the socket. There are several different ways to handle disconnects:

1) When the client disconnects it can send a message to the server saying that it''s about to disconnect.
2) Have the server use the Socket.Poll[^] method to get the current status. The return value from that can be combined with other information to determine if the socket is still open.
3) Have the server periodically send out a heartbeat message to the clients to test the connection. If it''s closed then the send will fail.

Using (1) will give you instantaneous notification when a client drops and (3) will handle clients that improperly drop their connections. (3) will also keep the client aware of the socket state since it can check for the heartbeats. (2) is similar to (3) except that nothing is sent to the clients so both client and server would have to actively check the socket state.


这篇关于了解插座中的断开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆