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();
        }
    }

 

转载于:https://www.cnblogs.com/wuxinjian/p/5278186.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值