Esempio n. 1
0
        //连接回调
        private void ConnectCallback(IAsyncResult ar)
        {
            try
            {
                SrvSocket.EndConnect(ar);    //结束挂起
                //myConnEvent.Set();            //设置有效信号表示已经收到数据
                OnClientConnected(new AsyncTCPClientEventArgs("已经成功连接到服务器", SrvSocket), "server cnonnect");
                //System.Diagnostics.Debug.WriteLine(String.Format("已经成功连接到服务器{0}!", _srvSocket.RemoteEndPoint.ToString()));
                //System.Diagnostics.Debug.WriteLine(String.Format("本地端接点为{0}!", _srvSocket.LocalEndPoint.ToString()));
                Rcvbuffer = new byte[MaxBufLen];     //创建一个接收缓冲区,用于存储所返回的数据

                ///每秒检测一次Socket是否正常连接
                if (IsAutoKeepAlive)
                {
                    SrvSocket.IOControl(IOControlCode.KeepAliveValues, KeepAlive(1, 60000, 1000), null);
                }

                AsyncCallback callback = new AsyncCallback(ReceiveCallback);
                SrvSocket.BeginReceive(Rcvbuffer, 0, Rcvbuffer.Length, SocketFlags.None, callback, SrvSocket);
            }
            catch (Exception ex)
            {
                //System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        }
Esempio n. 2
0
        //=========================================================
        /// <summary>
        /// 继续回调监听数据
        /// </summary>
        protected void ContinueReceiveCallback()
        {
            //重新创建一个接收缓冲区
            Rcvbuffer = new byte[SrvSocket.SendBufferSize];
            AsyncCallback callback = new AsyncCallback(ReceiveCallback);

            SrvSocket.BeginReceive(Rcvbuffer, 0, Rcvbuffer.Length, SocketFlags.None, callback, SrvSocket);
        }
Esempio n. 3
0
 private void ReceiveCallback(IAsyncResult ar)
 {
     try
     {
         int iDataLen = SrvSocket.EndReceive(ar);             //这是接收回来的数据长度
         if (iDataLen > 0)
         {
             _bReceiveData = new byte[iDataLen];               //存放已经接收返回的数据
             Array.Copy(Rcvbuffer, 0, _bReceiveData, 0, _bReceiveData.Length);
             //重新创建一个接收缓冲区
             //DataProcess(_bReceiveData);
             //ReceiveData = _bReceiveData;
             //ReceiveEvent.Set();
             OnDataReceived(new AsyncTCPClientEventArgs("data receive", SrvSocket), _bReceiveData);
             //AsyncCallback callback = new AsyncCallback(ReceiveCallback);
             //_srvSocket.BeginReceive(Rcvbuffer, 0, Rcvbuffer.Length, SocketFlags.None, callback, _srvSocket);
         }
         else
         {
             ShutdownAndDisposeSocket();
             OnSocketException(new AsyncTCPClientEventArgs("服务器已断开连接", SrvSocket), "server err");
         }
     }
     catch (SocketException ex)
     {
         ShutdownAndDisposeSocket();
         OnSocketException(new AsyncTCPClientEventArgs("服务器已断开连接", SrvSocket), "server err");
         LogHelpr.Error("服务器已断开连接:" + ex.ToString());
     }
     catch (Exception ex)
     {
         OnOtherException(new AsyncTCPClientEventArgs("服务器已断开连接", SrvSocket), "other err");
         LogHelpr.Error("服务器已断开连接:" + ex.ToString());
     }
     finally
     {
         if (SrvSocket != null && SrvSocket.Connected == true)
         {
             Rcvbuffer = new byte[MaxBufLen];
             AsyncCallback callback = new AsyncCallback(ReceiveCallback);
             SrvSocket.BeginReceive(Rcvbuffer, 0, Rcvbuffer.Length, SocketFlags.None, callback, SrvSocket);
         }
     }
 }