Esempio n. 1
0
        private void dataReceived(IAsyncResult result)
        {
            UdpDataReceivedEventArgs e;
            int received;

            byte[] data;
            try
            {
                received = socket.EndReceive(result);
                if (socket.Connected && (received != 0))
                {
                    data = new byte[received];
                    for (int i = 0; i < received; ++i)
                    {
                        data[i] = rxBuffer[i];
                    }
                    e = new UdpDataReceivedEventArgs(((IPEndPoint)socket.RemoteEndPoint).Address, data, false);
                    if (DataReceived != null)
                    {
                        DataReceived(e);
                    }
                    lastString = e.DataString;
                    for (int i = 0; i < rxBuffer.Length; ++i)
                    {
                        rxBuffer[i] = 0;
                    }
                    socket.BeginReceive(
                        rxBuffer,
                        0,
                        bufferSize,
                        SocketFlags.None,
                        new AsyncCallback(dataReceived),
                        socket);
                }
                else
                {
                    if (isOpen)
                    {
                        Disconnect();
                    }
                }
            }
            catch
            {
                if (isOpen)
                {
                    Disconnect();
                }
            }
        }
 private void dataReceived(IAsyncResult result)
 {
     UdpDataReceivedEventArgs e;
     int received;
     byte[] data;
     try
     {
         received = socket.EndReceive(result);
         if (socket.Connected && (received != 0))
         {
             data = new byte[received];
             for (int i = 0; i < received; ++i) data[i] = rxBuffer[i];
             e = new UdpDataReceivedEventArgs(((IPEndPoint)socket.RemoteEndPoint).Address, data, false);
             if (DataReceived != null)
                 DataReceived(e);
             lastString = e.DataString;
             for (int i = 0; i < rxBuffer.Length; ++i) rxBuffer[i] = 0;
             socket.BeginReceive(
                 rxBuffer,
                 0,
                 bufferSize,
                 SocketFlags.None,
                 new AsyncCallback(dataReceived),
                 socket);
         }
         else
         {
             if (isOpen)
                 Disconnect();
         }
     }
     catch
     {
         if (isOpen)
             Disconnect();
     }
 }