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 @ 2016-03-15 09:53 ForrestWu 阅读(898) 评论(0) 推荐(0) 编辑
摘要: Access: MSSQL: MySQL:   阅读全文
posted @ 2013-08-02 11:50 ForrestWu 阅读(661) 评论(0) 推荐(0) 编辑
摘要:   阅读全文
posted @ 2013-07-16 10:58 ForrestWu 阅读(280) 评论(0) 推荐(0) 编辑
摘要: 星期天为一周第一天 阅读全文
posted @ 2012-06-14 11:22 ForrestWu 阅读(277) 评论(0) 推荐(0) 编辑
摘要: Date (对象) Date 对象能够使你获得相对于国际标准时间(格林威治标准时间,现在被称为 UTC-Universal Coordinated Time)或者是 Flash 播放器正运行的操作系统的时间和日期。要使用Date对象的方法,你就必须先创建一个Date对象的实体(Instance)。 阅读全文
posted @ 2012-06-13 17:29 ForrestWu 阅读(536) 评论(0) 推荐(0) 编辑
摘要: 原因:此行为取决于基于内存已命中各种阈值时通知 Http.sys 驱动程序的事件。对于 Microsoft Windows Server 2003 和 Windows Vista,可用的非页面缓冲的池内存少于 20 MB 时,会发生此行为。这些值可能会更改在将来的版本的 Windows。 若要向注册 阅读全文
posted @ 2012-05-17 11:05 ForrestWu 阅读(409) 评论(0) 推荐(0) 编辑
点击右上角即可分享
微信分享提示