Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在TCP客户端(套接字)上在VB.NET中实现TCP KeepAlive_.net_Vb.net_Sockets_Tcpclient_Keep Alive - Fatal编程技术网

如何在TCP客户端(套接字)上在VB.NET中实现TCP KeepAlive

如何在TCP客户端(套接字)上在VB.NET中实现TCP KeepAlive,.net,vb.net,sockets,tcpclient,keep-alive,.net,Vb.net,Sockets,Tcpclient,Keep Alive,我想实现TCP KeepAlive,以便通过运行计时器来检查断开的连接 在我的例子中,我有一个TCP客户端(使用socket类)和一个第三方服务器(我对它没有控制权)。 如何在TCP客户端上使用TCP KeepAlive来检查连接状态 目前,我已经启用了TCP KeepAlive选项 tcpsocket.Connect(IPEndPoint) tcpSocket.SetSocketOption(SocketOptionLevel.Tcp,SocketOptionName.KeepAlive,1)

我想实现TCP KeepAlive,以便通过运行计时器来检查断开的连接

在我的例子中,我有一个TCP客户端(使用socket类)和一个第三方服务器(我对它没有控制权)。 如何在TCP客户端上使用TCP KeepAlive来检查连接状态

目前,我已经启用了TCP KeepAlive选项

tcpsocket.Connect(IPEndPoint)
tcpSocket.SetSocketOption(SocketOptionLevel.Tcp,SocketOptionName.KeepAlive,1);
在互联网上搜索,我发现这个过程应该可以工作,但我想知道如何在我的TCP客户端中使用它

Private Shared Function SetKeepAlive(ByRef tcpSocket As Socket, ByVal keepAliveTime As UInteger, ByVal keepAliveInterval As UInteger) As Boolean
  ' Pack three params into 12-element byte array; not sure about endian issues on non-Intel
  Dim SIO_KEEPALIVE_VALS(11) As Byte
  Dim keepAliveEnable As UInteger = 1
  If (keepAliveTime = 0 Or keepAliveInterval = 0) Then keepAliveEnable = 0
  ' Bytes 00-03 are 'enable' where '1' is true, '0' is false
  ' Bytes 04-07 are 'time' in milliseconds
  ' Bytes 08-12 are 'interval' in milliseconds
  Array.Copy(BitConverter.GetBytes(keepAliveEnable),  0, SIO_KEEPALIVE_VALS, 0, 4)
  Array.Copy(BitConverter.GetBytes(keepAliveTime),   0, SIO_KEEPALIVE_VALS, 4, 4)
  Array.Copy(BitConverter.GetBytes(keepAliveInterval), 0, SIO_KEEPALIVE_VALS, 8, 4)

  Try
   Dim result() As Byte = BitConverter.GetBytes(CUInt(0)) ' Result needs 4-element byte array?
   tcpSocket.IOControl(IOControlCode.KeepAliveValues, SIO_KEEPALIVE_VALS, result)
  Catch e As Exception
   Return False
  End Try
  Return True
 End Function
任何帮助都将不胜感激


PS:我是编程新手,请原谅我的编码错误。

最后我设法使用了TCP KeepAlive。 为了实现这一点,我刚刚调用了mainform_加载过程:

        tcpSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        tcpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, True)
        SetKeepAlive(tcpSocket, keepalivetime, keepaliveinterval)
        Try
            tcpSocket.Connect(ipEndPoint)
        Catch e As SocketException
            ...
        End Try
我已经和WireShark联系过了,我注意到了发送的包裹