简单的TCPSocket客户端使用案例(已入OPCommon)

做一个实体操作工具类(包含)

csharp 复制代码
public delegate void TcpClientReceivedEventHandler(TcpClientStateEventArgs args);

    public  class TCPSocketClient
    { 
        //接收委托
        public event TcpClientReceivedEventHandler TcpReceived;

        private CancellationTokenSource cts = new CancellationTokenSource();
        //
        public string _ip { get; set; }
        //
        public int _port { get; set; }
        //
        Socket socket = null;
        //
        Thread thread = null;
        public bool IsConnect = false;//控制回复
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        public TCPSocketClient(string ip, int port)
        {
            _ip = ip;
            _port = port;
        }
        /// <summary>
        /// 打开链接
        /// </summary>
        public void Open()
        {
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(_ip), _port);
            socket.Connect(endPoint);
           
            thread = new Thread(Receiver);
            thread.IsBackground = true;
            thread.Start();
        }
        /// <summary>
        /// 获取数据操作
        /// </summary>
        private void Receiver()
        {
            while (!cts.IsCancellationRequested)
            {
                byte[] buffer = new byte[1024 * 1024];
                int length = socket.Receive(buffer);
                if (length > 0)
                {
                    byte[] b = new byte[length];
                    Buffer.BlockCopy(buffer, 0, b, 0, length);
                    TcpClientStateEventArgs args = new TcpClientStateEventArgs();
                    args.buffer = b;
                    TcpReceived.Invoke(args);
                }
            }
        }
        /// <summary>
        /// 发送bytes
        /// </summary>
        /// <param name="data"></param>
        public void Send(byte[] data)
        {
            if (socket!=null )
            {
                socket.Send(data);
            }
          
        }
        /// <summary>
        /// 关闭当前链接
        /// </summary>
        public void Close()
        {
            IsConnect = false;
            if (socket!=null )
            {
                socket.Close();
            } 
            thread.Abort();
            socket = null;
            thread = null;
        }
    }
    /// <summary>
    /// Tcp状态事件参数类
    /// </summary>
    public class TcpClientStateEventArgs : EventArgs
    {
        public byte[] buffer = null;
    }
 
 

然后简单的使用方法就是

csharp 复制代码
  TCPSocketClient tcpClient = null;

        /// <summary>
        /// 其他测试
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (BTSocket.Text.Equals("链接Socket"))
            {
                BTSocket.Text = "断开";
                string ip = TBSocketIp.Text.Trim();
                int port = int.Parse(TBPort.Text.Trim());
                tcpClient = new TCPSocketClient(ip, port);
                tcpClient.Open();
                tcpClient.TcpReceived += TcpClient_TcpReceived;
            }
            else
            {
                BTSocket.Text = "链接Socket";
                tcpClient.Close();
            }
        }
        /// <summary>
        /// 接受数据
        /// </summary>
        /// <param name="args"></param>
        private void TcpClient_TcpReceived( TcpClientStateEventArgs args)
        {
            string str = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + ":";
            str += Encoding.UTF8.GetString(args.buffer);
            this.Invoke(new Action(() =>
            {
                LBRemark.Text = str;
            }));
        }
        /// <summary>
        /// 发送
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click_1(object sender, EventArgs e)
        {
            byte[] Bytes = OPCommon.ConvertType.HexStringToByteArray(TBSend.Text);
            tcpClient.Send(Bytes);
        } :
相关推荐
@#---20 分钟前
删除驱动精灵的详细过程
运维·服务器
boy快快长大28 分钟前
【Elasticsearch】同一台服务器部署集群
服务器·elasticsearch·jenkins
豪宇刘1 小时前
从三个维度了解 RPC(Remote Procedure Call,远程过程调用)
网络·网络协议·rpc
放氮气的蜗牛1 小时前
Linux命令终极指南:从入门到精通掌握150+核心指令
linux·运维·服务器
DC_BLOG2 小时前
Linux-Ansible模块进阶
linux·运维·服务器·ansible
Imagine Miracle2 小时前
【Deepseek】Linux 本地部署 Deepseek
linux·运维·服务器
SuperPurse2 小时前
linux下查看当前用户、所有用户的方法
linux·运维·服务器
风123456789~2 小时前
【Linux 专栏】echo命令实验
linux·运维·服务器
L耀早睡2 小时前
Linux中的查看命令
linux·运维·服务器
范桂飓4 小时前
RoCEv2 高性能传输协议与 Lossless 无损网络
网络