예제 #1
0
        private void ReceiveResult(IAsyncResult ar)
        {
            socket = (System.Net.Sockets.UdpClient)ar.AsyncState;
            IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0);

            recvBuffer = socket.EndReceive(ar, ref remote);
            dataPacker.UnPack(recvBuffer);
            socket.BeginReceive(ReceiveResult, socket);
        }
예제 #2
0
파일: UDP.cs 프로젝트: bmjoy/MiniFramework
        private void ReceiveResult(IAsyncResult ar)
        {
            udpClient = (UdpClient)ar.AsyncState;
            IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0);

            recvBuffer = udpClient.EndReceive(ar, ref remote);
            dataPacker.UnPack(recvBuffer);
            udpClient.BeginReceive(ReceiveResult, udpClient);
        }
예제 #3
0
 private void ReadResult(IAsyncResult ar)
 {
     System.Net.Sockets.TcpClient socket = (System.Net.Sockets.TcpClient)ar.AsyncState;
     if (socket.Client != null && socket.Connected)
     {
         NetworkStream stream     = socket.GetStream();
         int           recvLength = stream.EndRead(ar);
         if (recvLength > 0)
         {
             byte[] recvBytes = new byte[recvLength];
             Array.Copy(recvBuffer, 0, recvBytes, 0, recvLength);
             DataPacker dataPacker = new DataPacker();
             dataPacker.UnPack(recvBytes);
             stream.BeginRead(recvBuffer, 0, recvBuffer.Length, ReadResult, socket);
         }
     }
 }
예제 #4
0
        private void ReadResult(IAsyncResult ar)
        {
            System.Net.Sockets.TcpClient tcpClient = (System.Net.Sockets.TcpClient)ar.AsyncState;
            NetworkStream stream     = tcpClient.GetStream();
            int           recvLength = stream.EndRead(ar);

            if (recvLength <= 0)
            {
                Debug.Log("远程客户端:" + tcpClient.Client.RemoteEndPoint + "已经断开");
                remoteClients.Remove(tcpClient);
                tcpClient.Close();
                return;
            }
            byte[] recvBytes = new byte[recvLength];
            Array.Copy(recvBuffer, 0, recvBytes, 0, recvLength);
            dataPacker.UnPack(recvBytes);
            stream.BeginRead(recvBuffer, 0, recvBuffer.Length, ReadResult, tcpClient);
        }
예제 #5
0
 private void ReadResult(IAsyncResult ar)
 {
     tcpClient = (TcpClient)ar.AsyncState;
     if (tcpClient.Connected)
     {
         NetworkStream stream     = tcpClient.GetStream();
         int           recvLength = stream.EndRead(ar);
         if (recvLength <= 0)
         {
             Debug.Log("网络中断");
             MsgManager.Instance.SendMsg(MsgConfig.Net.ConnectAbort.ToString(), null);
             Close();
             return;
         }
         byte[] recvBytes = new byte[recvLength];
         Array.Copy(recvBuffer, 0, recvBytes, 0, recvLength);
         dataPacker.UnPack(recvBytes);
         stream.BeginRead(recvBuffer, 0, recvBuffer.Length, ReadResult, tcpClient);
     }
 }