Пример #1
0
        /// <summary>
        /// 客户端通道构造函数
        /// </summary>
        /// <param name="address">远程服务器端的主机名/IP地址/域名</param>
        /// <param name="port">远程服务器端主机端口号</param>
        public ClientHandler(string address, int port)
        {
            agree = new MessageAgreement(this)
            {
                OutTime = this._OutTime
            };
            HandlerType     = (int)_HandlerType.CHandler;
            _recvDataBuffer = new byte[DefaultBufferSize];
            IPAddress ipAddress = Dns.GetHostEntry(address).AddressList[0];

            _RemoteEndPoint = new IPEndPoint(ipAddress, port);
            ClientSocket    = new  Socket(_RemoteEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            ClientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, _sendoutTime);
            _HandlerID                = (int)ClientSocket.Handle;
            _isClose                  = true;
            agree.Error              += new MessageAgreement.ErrorEvent(Agree_Error);
            agree.ReceiveData        += new MessageAgreement.ReceiveDataEvent(Agree_ReceiveData);
            agree.ReceiveDataOutTime += new MessageAgreement.ReceiveDataOutTimeEvent(Agree_ReceiveDataOutTime);
        }
Пример #2
0
 /// <summary>
 /// 构造函数:服务器端创建一个与客户进行通信的通道
 /// </summary>
 /// <param name="CSocket">与客户端进行通信息Socket</param>
 public ClientHandler(Socket CSocket)
 {
     agree = new MessageAgreement(this)
     {
         OutTime = this._OutTime
     };
     HandlerType       = (int)_HandlerType.SHandler;
     this.ClientSocket = CSocket;
     _HandlerID        = (int)ClientSocket.Handle;
     _recvDataBuffer   = new byte[DefaultBufferSize];
     ClientSocket.BeginReceive(_recvDataBuffer, 0, _recvDataBuffer.Length, SocketFlags.None,
                               new AsyncCallback(ReceiveData), ClientSocket);
     ClientSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, _sendoutTime);
     _RemoteEndPoint           = (IPEndPoint)ClientSocket.RemoteEndPoint;
     _LocalEndPoint            = (IPEndPoint)ClientSocket.LocalEndPoint;
     agree.Error              += new MessageAgreement.ErrorEvent(Agree_Error);
     agree.ReceiveData        += new MessageAgreement.ReceiveDataEvent(Agree_ReceiveData);
     agree.ReceiveDataOutTime += new MessageAgreement.ReceiveDataOutTimeEvent(Agree_ReceiveDataOutTime);
     _isClose = false;
 }
Пример #3
0
 /// <summary>
 /// 关闭服务器端或客户端连接并释放所有关联的资源。
 /// </summary>
 public void Close()
 {
     try
     {
         _isClose           = true;
         _sessionTable      = null;
         this._HandlerID    = 0;
         this._isRun        = false;
         this._sessionTable = null;
         try
         {
             if (ClientSocket.Connected)
             {
                 ClientSocket.Shutdown(SocketShutdown.Both);
             }
         }
         catch { }
         this.ClientSocket.Close();
         agree.Close();
         agree = null;
     }
     catch
     {}
 }