TCP/IP 检测侦听器上是否有当前连接的客户端 [英] TCP/IP detect if there is a currently connected client on a listener

查看:36
本文介绍了TCP/IP 检测侦听器上是否有当前连接的客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让我的 TCP/IP 侦听器检测当前是否有连接的客户端?

Is there any way for my TCP/IP listener to detect if there are currently conencted client?

这是我的代码:

Public Sub StopListen()
    client.Close()
    tcpClientThread.Abort()
    server.Stop()
    SyncLock accessLock
        endThread = True
    End SyncLock
    Btn_Listen.Text = "Listen"
End Sub

该代码中的问题是每当用户按下 Stop Listen 按钮并且当前没有连接到我的监听器的 cients 时,就会发生错误.

The problem in that code is whenever the user pressed the Stop Listen button and there are currently no cients connected to my listener an error occurs.

我该如何做这样的事情?

How can I do something like this?

Public Sub StopListen()
   If thereIsConnectedClient Then
       client.Close()
   End If

   tcpClientThread.Abort()
   server.Stop()
   SyncLock accessLock
      endThread = True
   End SyncLock
   Btn_Listen.Text = "Listen"
End Sub

推荐答案

一般来说,SYNC-SYNC,ACK-ACK三路握手后,连接建立,TcpListener返回一个tcpClient.这个 tcpClient 可以保存在一个数组中.此数组中的元素数表示连接数.

Generally speaking, after the SYNC-SYNC,ACK-ACK three way handshake, the connection is established and TcpListener returns a tcpClient. This tcpClient can be kept in an array. The number of elements in this array represents the number of connections.

正常情况下,服务器和客户端交换数据包后,无论是服务器还是客户端都会发起FIN请求.交换 FIN-ACK、FIN-ACK 标志后,连接完成,tcpClient 可以从数组中删除.

In the normal scenario, after server and client exchange data packets, either the server or the client will initiate a FIN request. After the FIN-ACK,FIN-ACK flags are exchanged, connection finished and that tcpClient can be removed from the array.

现在如果有客户端没有主动关闭套接字,那么服务器可能需要一种机制来断开不活动的套接字.要么通过连接超时.或者定期发送一些空包并等待来自远程客户端的 ACK 来决定客户端是否仍然存在.

Now if there are clients not actively closing the socket, then the server may need an mechanism to disconnect the inactive socket. Either by having a timeout on the connection. Or periodically send some empty packet and wait for the ACK from the remote client to decide whether the client is still present.

这篇关于TCP/IP 检测侦听器上是否有当前连接的客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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