C#中 怎么检测Tcp网线断开?

在 C# 中,如果使用 TcpClient 或 TcpListener 这样的套接字进行通信,并且网络连接断开,不发送心跳是无法立即检测到断开的。这是因为 TCP 协议本身没有内置的机制来检测连接是否还活动中。

当使用 TCP 进行通信时,通常是通过发送和接收数据来维持连接的活跃状态。如果没有数据传输,并且不发送心跳包,那么时间一过,连接就会被认为是空闲的,而不管实际上该连接是否仍然存在。

为了检测到连接断开,你可以采取以下方法之一:

  1. 发送心跳包:发送定期的心跳包来保持连接活动。如果一段时间内没有接收到心跳包,就可以假定连接已断开。

  2. 设置超时:在发送和接收数据时,设置超时时间。如果超过指定的时间仍未收到响应,说明连接可能已经断开。

  3. 使用 Keep-Alive 选项:在套接字连接时,可以启用 Keep-Alive 选项。这样,操作系统将自动发送心跳包,以检测连接的活跃性。

  4. 使用异步操作:使用异步操作来处理数据的发送和接收。这样,你可以通过检查异步操作的状态来确定连接是否仍然有效。

当使用 C# 进行 TCP 连接时,下面是一些示例代码,演示如何使用各种方法来检测连接是否断开。

  1. 发送心跳包:(好用)
using System;
using System.Net.Sockets;
using System.Threading;

class TcpClientExample
{
    static void Main()
    {
        string serverIP = "127.0.0.1";
        int serverPort = 12345;
        TcpClient client = new TcpClient(serverIP, serverPort);

        // 在一个单独的线程中发送心跳包
        var heartbeatThread = new Thread(() =>
        {
            while (true)
            {
                Thread.Sleep(5000); // 5 秒发送一次心跳包

                // 发送心跳包数据
                byte[] heartbeatData = { 0x01, 0x02, 0x03 }; // 根据实际需求自定义
                NetworkStream stream = client.GetStream();
                stream.Write(heartbeatData, 0, heartbeatData.Length);
            }
        });
        heartbeatThread.Start();

        // 在主线程中监听服务器响应或检测连接断开
        try
        {
            byte[] buffer = new byte[1024];
            NetworkStream stream = client.GetStream();

            while (true)
            {
                int bytesRead = stream.Read(buffer, 0, buffer.Length);

                if (bytesRead == 0)
                {
                    // 连接断开
                    Console.WriteLine("连接已断开");
                    break;
                }

                // 处理服务器返回的数据
                // ...
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("发生错误: " + ex.Message);
        }
    }
}
  1. 设置超时:(好用)
using System;
using System.Net.Sockets;

class TcpClientExample
{
    static void Main()
    {
        string serverIP = "127.0.0.1";
        int serverPort = 12345;
        TcpClient client = new TcpClient(serverIP, serverPort);
        client.ReceiveTimeout = 5000; // 设置接收超时时间为 5 秒

        try
        {
            byte[] buffer = new byte[1024];
            NetworkStream stream = client.GetStream();

            while (true)
            {
                int bytesRead = stream.Read(buffer, 0, buffer.Length);

                // ...

                if (bytesRead == 0)
                {
                    // 连接断开
                    Console.WriteLine("连接已断开");
                    break;
                }
            }
        }
        catch (SocketException ex)
        {
            if (ex.SocketErrorCode == SocketError.TimedOut)
            {
                Console.WriteLine("连接超时");
            }
            else
            {
                Console.WriteLine("发生错误: " + ex.Message);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("发生错误: " + ex.Message);
        }
    }
}
  1. 使用 Keep-Alive 选项:(不好用)
using System;
using System.Net.Sockets;

class TcpClientExample
{
    static void Main()
    {
        string serverIP = "127.0.0.1";
        int serverPort = 12345;
        TcpClient client = new TcpClient(serverIP, serverPort);
        client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);

        try
        {
            // 设置 Keep-Alive 参数
            // 这里使用默认的 Keep-Alive 参数,也可以通过设置 TcpKeepAlive 类的属性来自定义参数
            client.Client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, 5000); // 5 秒钟发送一次心跳包
            client.Client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, 1000); // 1 秒钟未收到 ACK 后重试
            client.Client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, 3); // 重试次数为 3

            byte[] buffer = new byte[1024];
            NetworkStream stream = client.GetStream();

            while (true)
            {
                int bytesRead = stream.Read(buffer, 0, buffer.Length);

                // ...

                if (bytesRead == 0)
                {
                    // 连接断开
                    Console.WriteLine("连接已断开");
                    break;
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("发生错误: " + ex.Message);
        }
    }
}
  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天hous

你的鼓励是我最大动力~谢谢啦!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值