Esempio n. 1
0
 private void ReceiveTcpCB(IAsyncResult result)
 {
     try
     {
         if (socket.Connected)
         {
             if (socket.EndReceive(result) > 0)
             {
                 HandleData.Read(id, buffer);
                 if (socket.Connected)
                 {
                     socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveTcpCB, socket);
                 }
             }
             else
             {
                 //TODO: Close connection of client that send 0 data
                 Disconnet();
             }
         }
     }
     catch (Exception ex)
     {
         //CloseConnection();
         Disconnet();
         Console.WriteLine($"\n[TCP] Receive Error:\n{ex.ToString()}\n");
     }
 }
Esempio n. 2
0
 private void ReceiveUdpCB(IAsyncResult result)
 {
     try
     {
         if (endPoint == null)
         {
             return;
         }
         Server.socket.EndReceiveFrom(result, ref endPoint);
         HandleData.Read(Guid.Empty, Server.buffer);
         Server.socket.BeginReceiveFrom(Server.buffer, 0, Server.buffer.Length, SocketFlags.None, ref endPoint, ReceiveUdpCB, Server.socket);
     }
     catch (Exception ex)
     {
         //CloseConnection();  // should we close tcp connection when udp fail?
         Console.WriteLine($"\n[UDP] Receive Error:\n{ex.ToString()}\n");
     }
 }