程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> TcpClient,c#tcpclient

TcpClient,c#tcpclient

編輯:C#入門知識

TcpClient,c#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();
        }
    }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved