c# - 在 C# 中从服务器轮询连接的客户端

标签 c# sockets networking tcp connect

我正在尝试轮询来自连接到我的服务器的客户端的连接。

当您使用 poll 时,您需要给它一个套接字连接,但在服务器端,套接字绑定(bind)到它自己的 IP 地址和特定端口。让另一个套接字连接到同一个端口但使用客户端的 IP 地址将不起作用,因为您不能在同一个套接字上有多个连接。

我只是想知道什么是不断检查客户端是否仍连接到服务器以及何时断开连接的好方法?

我在考虑某种超时检查之类的。我只是想知道是否有任何通用或正确的方法来实现这一目标。

我试过 Socket.Poll 但它似乎没有达到我想要的效果。

重申一下我的问题,您如何使用 C# 中的 TCP 套接字检查客户端是否连接到服务器端?

最佳答案

socket.Receive 只会返回 0。 来自 MSDN

If you are using a connection-oriented Socket, the Receive method will read as much data as is available, up to the size of the buffer. If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.

还有Connected Socket 类中的属性(如果需要)。

关于c# - 在 C# 中从服务器轮询连接的客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24876180/

相关文章:

c - 如何在 Linux 中将套接字链接到 PID?

c - c中的持久tcp套接字连接

c# - 向 Youtube 发送缓慢的 HttpClient 请求

networking - 判断 net.Listener 是否已死

c# - 如何使用超时取消 TaskCompletionSource

c# - 我想比较 2 个哈希集并找出差异

c# - 在C#和Java中,Object[]和String[]之间的关系是什么?

c# - 在 C# 中的基本构造函数之前执行派生构造函数

php - 如何使用 PHP 创建到同一主机的多个持久套接字连接 TCP/IP

c++ - 为什么 CreateFile 无法通过网络共享打开文件?