Cause of high UDP package loss on localhost?

后端 未结 1 476
我在风中等你
我在风中等你 2020-12-19 05:46

In my WPF 4.0 application, I have a UDP listener implemented as shown below. On my Windows 7 PC, I\'m running both server and client on local

相关标签:
1条回答
  • 2020-12-19 05:51

    To avoid the thread block issue, try this approach that uses the newer IO Completion port receive method:

    private void OnReceive(object sender, SocketAsyncEventArgs e)
    {
    TOP:
        if (e != null)
        {
            int length = e.BytesTransferred;
            if (length > 0)
            {
                FireBytesReceivedFrom(Datagram, length, (IPEndPoint)e.RemoteEndPoint);
            }
            e.Dispose(); // could possibly reuse the args?
        }
        Socket s = Socket;
        if (s != null && RemoteEndPoint != null)
        {
            e = new SocketAsyncEventArgs();
            try
            {
                e.RemoteEndPoint = RemoteEndPoint;
                e.SetBuffer(Datagram, 0, Datagram.Length); // don't allocate a new buffer every time
                e.Completed += OnReceive;
                // this uses the fast IO completion port stuff made available in .NET 3.5; it's supposedly better than the socket selector or the old Begin/End methods
                if (!s.ReceiveFromAsync(e)) // returns synchronously if data is already there
                    goto TOP; // using GOTO to avoid overflowing the stack
            }
            catch (ObjectDisposedException)
            {
                // this is expected after a disconnect
                e.Dispose();
                Logger.Info("UDP Client Receive was disconnected.");
            }
            catch (Exception ex)
            {
                Logger.Error("Unexpected UDP Client Receive disconnect.", ex);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题