ReceiveFromAsync Example

Does anyone have an example of how ReceiveFromAsync works with regards to UDP? I could not find any sample code. I can find some examples of TCP, but msdn said: "The ReceiveFromAsync method is used primarily to receive data on a socket without establishing a connection."

Thanks Nick

+3
source share
2 answers

Perhaps it might be easier to use the async BeginReceive () method of UdpClient?

http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.beginreceive.aspx

+1
source

If performance is not a concern, a quick and easy approach could be the UdpClient ReceiveAsync method:

https://msdn.microsoft.com/de-de/library/system.net.sockets.udpclient.receiveasync(v=vs.110).aspx

(UdpReceiveResult) , . :

private async void ReceiveBytesAsync(IPEndPoint filter)
{
    UdpReceiveResult receivedBytes  = await this._udpClient.ReceiveAsync();

    if (filter != null)
    {
        if (receivedBytes.RemoteEndPoint.Address.Equals(filter.Address) &&
                (receivedBytes.RemoteEndPoint.Port.Equals(filter.Port)))
        {
            // process received data
        }
    }
}
+1

Source: https://habr.com/ru/post/1750561/


All Articles