基于C#的以太网通讯实现:TcpClient异步通讯详解

基于C#的以太网通讯实现:TcpClient异步通讯详解

在现代工业控制和物联网应用中,以太网通讯是一种常见的数据传输方式。本文将介绍如何使用C#实现基于TCP协议的以太网通讯,并通过异步编程提高通讯效率。我们将使用TcpClient类来实现客户端与服务器的连接、数据发送和接收,并详细讲解关键技术点。

1. 概述

以太网通讯基于TCP/IP协议,是一种可靠的、面向连接的通讯方式。在C#中,System.Net.Sockets.TcpClient类提供了对TCP协议的支持,可以方便地实现客户端与服务器之间的通讯。本文将使用异步编程模型(APM)来处理连接、发送和接收数据,以提高程序的响应性和性能。

2. 关键技术点

2.1 TcpClient类

TcpClient是C#中用于实现TCP客户端通讯的核心类。它封装了底层的Socket操作,提供了简单易用的接口。

2.2 数据缓冲区

在通讯过程中,数据通过字节数组(byte[])进行传输。合理设置缓冲区大小可以提高数据传输效率。

2.3 连接超时处理

在网络通讯中,连接超时是一个常见问题。通过设置超时时间,可以避免程序长时间等待无响应的服务器。

3. 代码实现

以下是基于TcpClient的异步通讯实现代码,关键技术点已标识。

3.1 TcpClientAsyncTool类

csharp 复制代码
    public class TcpClientAsyncTool
    {
        TcpClient tcpClient;
        public bool isConnected = false;
        IPAddress iPAddress;
        int port;
        int connectTimeout;
        byte[] receiveBuffer = new byte[1024];
        public TcpClientAsyncTool(string ip, int port, int connectTimeout = 2000)
        {
            tcpClient = new TcpClient();
            this.iPAddress = IPAddress.Parse(ip);
            this.port = port;
            this.connectTimeout = connectTimeout;

        }
        /// <summary>
        /// 连接服务器
        /// </summary>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public bool Connect(Action connectDelegate, out string errorMsg)
        {
            bool result = false;
            errorMsg = string.Empty;
            try
            {
                IAsyncResult asyncResult = tcpClient.BeginConnect(iPAddress, port, null, null);
                connectDelegate();
                bool success = asyncResult.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(connectTimeout));//设置连接超时时间为2秒
                tcpClient.EndConnect(asyncResult);
                result = true;
                isConnected = result;
            }
            catch (Exception ex)
            {
                errorMsg = ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 断开连接
        /// </summary>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public bool DisConnect(out string errorMsg)
        {
            bool result = false;
            errorMsg = string.Empty;
            try
            {
                tcpClient.Close();
                isConnected = result;
            }
            catch (Exception ex)
            {
                errorMsg = ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public bool SendData(byte[] sendBytes, out string errorMsg)
        {
            bool result = false;
            errorMsg = string.Empty;
            try
            {
                NetworkStream networkStream = tcpClient.GetStream();
                IAsyncResult asyncResult = networkStream.BeginWrite(sendBytes, 0, sendBytes.Length, null, null);
                networkStream.EndWrite(asyncResult);
                result = true;
            }
            catch (Exception ex)
            {
                errorMsg = ex.Message + ex.StackTrace;
            }
            return result;
        }

        /// <summary>
        /// 接收数据
        /// </summary>
        /// <param name="result"></param>
        /// <param name="errorMsg"></param>
        /// <returns></returns>
        public byte[] ReceiveData(out bool result, out string errorMsg)
        {
            result = false;
            errorMsg = string.Empty;
            byte[] readByteArray = null;
            try
            {
                NetworkStream networkStream = tcpClient.GetStream();
                IAsyncResult iAsyncResult = networkStream.BeginRead(receiveBuffer, 0, receiveBuffer.Length, null, null);
                int readBytes = networkStream.EndRead(iAsyncResult);
                readByteArray = new byte[readBytes];
                Array.Copy(receiveBuffer, readByteArray, readBytes);
                result = true;
            }
            catch (Exception ex)
            {
                errorMsg = ex.Message + ex.StackTrace;
            }
            return readByteArray;
        }
    }

3.2 使用示例

以下是使用TcpClientAsyncTool类的示例代码,展示了如何连接服务器、发送和接收数据。

3.2.1 socket连接,端口号10010

csharp 复制代码
    tcpClient1 = new TcpConnect.TcpClientAsyncTool(controlBoxWithID1.IP, 10010); // 初始化TcpClient
    tasks.Add(Task.Run(async () =>
    {
        if (tcpClient1.Connect(action, out errorMsg)) // 连接服务器
        {
            isConnected1 = true;
            await Task.Run(new Action(ReaceiveData)); // 启动数据接收任务
        }
    }));

3.2.2 数据接收

提前声明:TcpConnect.TcpClientAsyncTool tcpClient1; TcpClient客户端1

csharp 复制代码
private void ReaceiveData()
{
    while (true)
    {
        try
        {
            bool result = false;
            string errorMsg = string.Empty;
            byte[] readReadBytes = tcpClient1.ReceiveData(out result, out errorMsg); // 接收数据
            if (readReadBytes != null)
            {
             //处理接收数据信息readReadBytes 
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Failed to connect to power supply: {ex.Message}"); // 捕获异常并输出错误信息
        }
        if (isConnected1 == false)
        {
            break; // 如果连接断开,退出循环
        }
    }
}

3.2.2 数据发送

csharp 复制代码
        public void SendData1(byte[] message)
        {
            string errorMsg = string.Empty;
            if (isConnected1)
            {
                tcpClient1.SendData(message, out errorMsg);
            }
        }

4. 关键技术解析

4.1 异步连接

通过BeginConnect和EndConnect方法实现异步连接,避免了主线程阻塞。使用AsyncWaitHandle.WaitOne设置超时时间,防止连接无响应的服务器。

4.2 异步发送和接收数据

使用BeginWrite/EndWrite和BeginRead/EndRead实现异步数据发送和接收,确保通讯过程不会阻塞主线程。

4.3 数据缓冲区

通过byte[]数组作为数据缓冲区,接收到的数据会被存储在该数组中。合理设置缓冲区大小可以提高数据传输效率。

4.4 连接状态管理

通过isConnected变量管理连接状态,确保在连接断开时及时停止数据接收任务。

5. 总结

本文介绍了如何使用C#实现基于TCP协议的以太网通讯,并通过异步编程模型提高了通讯效率。关键技术点包括TcpClient的使用、异步编程模型、数据缓冲区和连接超时处理。通过本文的代码示例,您可以快速实现一个可靠的以太网通讯客户端。

希望这篇文章对您有所帮助!如果有任何问题,欢迎在评论区留言讨论。

相关推荐
小小码农Come on5 分钟前
QT中窗口位置、相对位置、绝对位置
android·开发语言·qt
diediedei7 分钟前
C++中的适配器模式变体
开发语言·c++·算法
郝学胜-神的一滴15 分钟前
Python中的Mixin继承:灵活组合功能的强大模式
开发语言·python·程序人生
叫我:松哥16 分钟前
基于python强化学习的自主迷宫求解,集成迷宫生成、智能体训练、模型评估等
开发语言·人工智能·python·机器学习·pygame
晚霞的不甘17 分钟前
Flutter for OpenHarmony 创意实战:打造一款炫酷的“太空舱”倒计时应用
开发语言·前端·flutter·正则表达式·前端框架·postman
WKP941821 分钟前
线程并行控制CompletableFuture
java·开发语言
飞机和胖和黄26 分钟前
考研之C语言第二周作业
c语言·开发语言·考研
输出输入33 分钟前
MT4 EA 设计一次一单方法
开发语言
一起养小猫35 分钟前
OpenHarmony 实战中的 Flutter:深入理解 Widget 核心概念与底层原理
开发语言·flutter
盐真卿40 分钟前
python第四部分:模块(每日更新)
开发语言·python