NetworkStream.Read的替代方案,表示远程主机已关闭连接?

时间:2010-01-03 01:49:00

标签: c# tcp connection tcpclient networkstream

关于使用TcpClient类处理TCP / IP连接,是否有替代方法可以检查远程主机是否已关闭连接,而不是等待NetworkStream.Read方法返回0 ?

1 个答案:

答案 0 :(得分:6)

您可以在IOControlCode.KeepAliveValues上使用TcpClient.Client来保证至少在指定的时间间隔内进行保持活动检查,然后检查TcpClient.Client.Connected属性。

如何使用它的示例:

struct tcp_keepalive
{
    public int OnOff;
    public int KeepAliveTime;
    public int KeepAliveInterval;

    public unsafe byte[] Buffer
    {
        get
        {
            var buf = new byte[sizeof(tcp_keepalive)];
            fixed(void* p = &this) Marshal.Copy(new IntPtr(p), buf, 0, buf.Length);
            return buf;
        }
    }
};

static void KeepAliveTest()
{
    using(var c = new TcpClient())
    {
        c.Connect("www.google.com", 80);
        var s = c.Client;
        var ka = new tcp_keepalive();
        ka.OnOff = 1; // enable
        ka.KeepAliveTime = 1000 * 60; // 60 seconds of inactivity allowed
        ka.KeepAliveInterval = 1000; // 1 second interval on keep-alive checks (default)
        s.IOControl(IOControlCode.KeepAliveValues, ka.Buffer, null);
        var ns = c.GetStream();
        Console.WriteLine("Connected to " + s.RemoteEndPoint);
        while(true)
        {
            SocketError se;
            s.Blocking = false;
            s.Receive(new byte[0], 0, 0, SocketFlags.Peek, out se);
            s.Blocking = true;
            if(!s.Connected)
            {
                // se==SocketError.ConnectionReset||SocketError.NetworkReset if the connection was closed because of a keep-alive check
                Console.WriteLine("Socket disconnected: " + se);
                break;
            }
            // do other stuff
            if(ns.DataAvailable) ns.Read(new byte[100], 0, 100);
            else Thread.Sleep(10);
        }
    }
}