TcpClient

复制代码
public class TcpClientSession
    {
        protected TcpClient Client { get; set; }
        /// <summary>
        /// 远程地址
        /// </summary>
        protected IPEndPoint RemoteEndPoint { get; set; }
        /// <summary>
        /// 是否已经连接
        /// </summary>
        public bool IsConnected { get; private set; }
        /// <summary>
        /// 接收缓存区大小
        /// </summary>
        public int ReceiveBufferSize = 1024;
        /// <summary>
        /// 数据流对象
        /// </summary>
        protected NetworkStream _NetStream;

        /// <summary>
        /// 已连接事件
        /// </summary>
        public event Action OnConnected;
        /// <summary>
        /// 断开事件
        /// </summary>
        public event Action OnClosed;

        public TcpClientSession(IPEndPoint remoteEndPoint)
        {
            if (remoteEndPoint == null)
                throw new ArgumentNullException("remoteEndPoint");

            RemoteEndPoint = remoteEndPoint;
        }

        public TcpClientSession(string server, int port)
        {
            if (server != null && port > 0)
            {
                if (!Regex.IsMatch(server, @"(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})"))
                {
                    try
                    {
                        IPHostEntry ipHostEntry = System.Net.Dns.GetHostEntry(server);
                        server = ipHostEntry.AddressList[0].ToString();
                    }
                    catch (Exception)
                    {
                        throw new ArgumentNullException("远程IP地址或域名不正确");
                    }
                }

                RemoteEndPoint = new IPEndPoint(IPAddress.Parse(server), port);
            }
            else
                throw new ArgumentNullException("remoteEndPoint");
        }

        public void Connect()
        {
            Client = new TcpClient(RemoteEndPoint.AddressFamily);
            Client.ReceiveBufferSize = ReceiveBufferSize;
            Client.Connect(RemoteEndPoint);
            if (Client.Client.Connected)
            {
                _NetStream = Client.GetStream();

                IsConnected = true;
                if (this.OnConnected != null)
                    OnConnected();
            }
            else
                throw new Exception("Unable to connect to a remote device");

        }

        public byte[] Received()
        {
            if (Client.Client.Connected)
            {
                byte[] buffer = null;
                buffer = new byte[ReceiveBufferSize];
                _NetStream.Read(buffer, 0, buffer.Length);
                return buffer;
            }
            else
            {
                Close();
            }
            return null;
        }

        public void Send(byte[] bs)
        {
            if (Client.Client.Connected)
            {
                _NetStream.Write(bs, 0, bs.Length);
            }
            else
            {
                Close();
            }
        }

        public void Close()
        {
            if (Client.Client.Connected)
            {
                Client.Close();
                _NetStream.Close();
            }

            IsConnected = false;
            if (this.OnClosed != null)
                OnClosed();
        }
    }
复制代码

 

posted @   ForrestWu  阅读(896)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 上周热点回顾(1.20-1.26)
· 【译】.NET 升级助手现在支持升级到集中式包管理
点击右上角即可分享
微信分享提示