/// <summary> 当接收到数据之后的回调函数 </summary>
        private void ReadCallback(IAsyncResult ar)
        {
            if (stateOne == null)
            {
                return;
            }
            Socket handler = stateOne.WorkSocket;

            try
            {
                int bytesRead = handler.EndReceive(ar);
                if (bytesRead > 0)
                {
                    //MessageBox.Show(stateOne.Buffer[0].ToString());
                    //接收完成之后对数组进行重置
                    byte[] haveDate = ReceiveDateOne.DateOneManage(stateOne, bytesRead);

                    handler.BeginReceive(stateOne.Buffer, 0, stateOne.Buffer.Length, 0, new AsyncCallback(ReadCallback), stateOne);
                    TcpDateOne(stateOne, haveDate);
                }
                else
                {
                    handler.BeginReceive(stateOne.Buffer, 0, stateOne.Buffer.Length, 0, new AsyncCallback(ReadCallback), stateOne);
                }
            }
            catch (Exception Ex)
            {
                //当突然断开的时候
                lostClient(Ex.Message);
            }
        }
        /// <summary> 当接收到数据之后的回调函数 </summary>
        private void ReadCallback(IAsyncResult ar)
        {
            TcpState stateOne = (TcpState)ar.AsyncState;

            Socket handler = stateOne.WorkSocket;

            try
            {
                int bytesRead = handler.EndReceive(ar);

                if (bytesRead > 0)
                {
                    byte[] haveDate = ReceiveDateOne.DateOneManage(stateOne, bytesRead);//接收完成之后对数组进行重置
                    handler.BeginReceive(stateOne.Buffer, 0, stateOne.Buffer.Length, 0, new AsyncCallback(ReadCallback), stateOne);
                    TcpDateOne(stateOne, haveDate);
                }
                else
                {
                    handler.BeginReceive(stateOne.Buffer, 0, stateOne.Buffer.Length, 0, new AsyncCallback(ReadCallback), stateOne);
                }
            }
            catch (Exception Ex)
            {
                int i = Ex.Message.IndexOf("远程主机强迫关闭了一个现有的连接");

                if (stateOne != null && i != -1)
                {
                    socketRemove(stateOne, Ex.Message);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// 接收到信息的回调函数
        /// </summary>
        /// <param name="iar"></param>
        private void EndReceiveFromCallback(IAsyncResult iar)
        {
            int byteRead = 0;

            try
            {
                //完成接收
                byteRead = state.WorkSocket.EndReceiveFrom(iar, ref state.RemoteEP);
            }
            catch {}
            if (byteRead > 0)
            {
                state.IpEndPoint = (IPEndPoint)state.RemoteEP;
                byte[] haveDate = ReceiveDateOne.DateOneManage(state, byteRead);    //接收完成之后对数组进行重置
                BeginReceiveFrom();
                int havePort = UdpPortSetGet.GetPort(ref haveDate);
                if (havePort != 0)
                {
                    state.IpEndPoint.Port = havePort;
                }
                StateCode statecode = ReceiveDateDistribution.Distribution(haveDate);
                codeManage(state, statecode);
            }
            else
            {
                BeginReceiveFrom();
            }
        }