示例#1
0
        private void OnDataReceived(IAsyncResult asyn)
        {
            CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState;
            Socket        socket    = theSockId.thisSocket;

            if (!socket.Connected)
            {
                return;
            }

            try
            {
                int iRx;
                try
                {
                    iRx = socket.EndReceive(asyn);
                }
                catch (SocketException)
                {
                    Debug.Write("Apperently client has been closed and connot answer.");

                    OnConnectionDroped(socket);
                    return;
                }

                if (iRx == 0)
                {
                    Debug.Write("Apperently client socket has been closed.");
                    // If client socket has been closed (but client still answers)-
                    // EndReceive will return 0.
                    OnConnectionDroped(socket);
                    return;
                }

                byte[] bytes = theSockId.dataBuffer;

                RaiseMessageRecived(bytes);

                WaitForData(m_socWorker);
            }
            catch (Exception ex)
            {
                Debug.Fail(ex.ToString(), "OnClientConnection: Socket failed");
            }
        }
示例#2
0
 private void WaitForData(Socket soc)
 {
     try
     {
         if (pfnWorkerCallBack == null)
         {
             pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
         }
         CSocketPacket theSocPkt = new CSocketPacket(BufferLength);
         theSocPkt.thisSocket = soc;
         // now start to listen for any data...
         soc.BeginReceive(
             theSocPkt.dataBuffer,
             0,
             theSocPkt.dataBuffer.Length,
             SocketFlags.None,
             pfnWorkerCallBack,
             theSocPkt);
     }
     catch (SocketException sex)
     {
         Debug.Fail(sex.ToString(), "WaitForData: Socket failed");
     }
 }