コード例 #1
0
 public override void Receive()
 {
     try
     {
         var state = new ControlState
         {
             CurrentSocket = ClientSocket,
             Buffer        = new byte[MaxBufferSize],
             BufferSize    = MaxBufferSize,
             StreamBuffer  = new MemoryStream()
         };
         var ep = new IPEndPoint(IPAddress.Any, 0) as EndPoint;
         ClientSocket.BeginReceiveFrom(state.Buffer, 0, MaxBufferSize, 0, ref ep, OnReceiveFromCallback, state);
         OnReportingStatus(StatusCode.Info, "Started receiving bytes via UDP socket");
     }
     catch (ObjectDisposedException)
     {
     }
     catch (SocketException socketException)
     {
         OnCaughtException(socketException, EventCode.Receive);
     }
     catch (Exception e)
     {
         OnCaughtException(e, EventCode.Other);
     }
 }
コード例 #2
0
ファイル: UdpClient.cs プロジェクト: gkurbesov/C4C.Socket
 /// <summary>
 /// Делегат вызова принятия данных  (калбэк)
 /// </summary>
 /// <param name="result"></param>
 private void ReceiveCallback(IAsyncResult result)
 {
     try
     {
         if (ClientSocket != null && ClientSocket.Connected)
         {
             int read_size = ClientSocket.EndReceiveFrom(result, ref ServerEndPoint);
             if (read_size > 0)
             {
                 BufferBuilder.Append(Buffer, read_size);
                 if (ClientSocket.Available <= 0)
                 {
                     //Вызываем событие по окнчанию чтения данных от сокета
                     CallReceive(BufferBuilder.Data);
                     BufferBuilder.Clear();
                 }
                 ClientSocket.BeginReceiveFrom(
                     Buffer, 0,
                     Buffer.Length, SocketFlags.None, ref ServerEndPoint,
                     new AsyncCallback(ReceiveCallback),
                     null);
             }
             else
             {
                 Disconnect();
             }
         }
         else
         {
             Disconnect();
         }
     }
     catch (SocketException ex)
     {
         CallErrorClient(ClientErrorType.ReceiveDataError, ex.Message);
         Disconnect();
     }
     catch (Exception ex)
     {
         CallErrorClient(ClientErrorType.ReceiveDataError, ex.Message);
         Disconnect();
     }
 }