what is the best way to do keep alive socket checking in .NET?

前端 未结 3 1572
别那么骄傲
别那么骄傲 2020-12-09 19:53

I am looking for a way to do a keep alive check in .NET. The scenario is for both UDP and TCP.

Currently in TCP what I do is that one side connects and when there is

相关标签:
3条回答
  • 2020-12-09 20:00

    Since you cannot use the blocking (synchronous) receive, you will have to settle for the asynchronous handling. Fortunately that's quite easy to do with .NET. Look for the description of BeginReceive() and EndReceive(). Or check out this article or this.

    As for the timeout behaviour I found no conclusive description of this. Since it's not documented otherwise you have to assume that it's the intended behaviour.

    0 讨论(0)
  • 2020-12-09 20:02

    If you literally mean "KeepAlive", try the following.

        public static void SetTcpKeepAlive(Socket socket, uint keepaliveTime, uint keepaliveInterval)
        {
            /* the native structure
            struct tcp_keepalive {
            ULONG onoff;
            ULONG keepalivetime;
            ULONG keepaliveinterval;
            };
            */
    
            // marshal the equivalent of the native structure into a byte array
            uint dummy = 0;
            byte[] inOptionValues = new byte[Marshal.SizeOf(dummy) * 3];
            BitConverter.GetBytes((uint)(keepaliveTime)).CopyTo(inOptionValues, 0);
            BitConverter.GetBytes((uint)keepaliveTime).CopyTo(inOptionValues, Marshal.SizeOf(dummy));
            BitConverter.GetBytes((uint)keepaliveInterval).CopyTo(inOptionValues, Marshal.SizeOf(dummy) * 2);
    
            // write SIO_VALS to Socket IOControl
            socket.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null);
        }
    

    Note the time units are in milliseconds.

    0 讨论(0)
  • 2020-12-09 20:03

    According to MSDN, a SocketException thrown when ReceiveTimeout is exceeded in Receive call will not close the socket. There is something else going on in your code.

    Check the caught SocketException details - maybe it's not a timeout after all. Maybe the other side of the connection shuts down the socket.

    Consider enabling network tracing to diagnose the exact source of your problems: look for "Network Tracing" on MSDN (can't provide you with a link, since right now MSDN is down).

    0 讨论(0)
提交回复
热议问题