I have a UdpClient
listening on IPAddress.Any
. When I receive data (currently using ReceiveAsync
, but I can change this if needed), the returned UdpReceiveResult
has a field with the remote end point (where the message was sent from) but not the local one. How can I know which ip address it was received on, since I am listening on all of them?
Asked
Active
Viewed 1,175 times
1

Baruch
- 20,590
- 28
- 126
- 201
1 Answers
2
I don't know if the same functionality is provided by the *Async methods, but here is an implementation that gives you the receiving IP Address and the network interface number which received the UDP packet.
By using the underlying Socket
s (Begin/End)ReceiveMessageFrom methods, we can get more information about the packet we have received. The code documentation for its two significant parameters are:
// endPoint:
// The source System.Net.EndPoint.
//
// ipPacketInformation:
// The System.Net.IPAddress and interface of the received packet.
I hope this helps.
static UdpClient client;
static byte[] byts = new byte[8192];
static void ReceiveUDP(IAsyncResult asyncResult)
{
EndPoint ipEndpoint = new IPEndPoint(IPAddress.Any, 9876);
IPPacketInformation packetInformation;
SocketFlags socketFlags = SocketFlags.None;
int numberOfBytesReceived = client.Client.EndReceiveMessageFrom(asyncResult, ref socketFlags, ref ipEndpoint, out packetInformation);
Console.WriteLine
(
"Received {0} bytes\r\nfrom {1}\r\nReceiving IP Address: {2}\r\nNetwork Interface #{3}\r\n************************************",
numberOfBytesReceived,
ipEndpoint,
packetInformation.Address,
packetInformation.Interface
);
EndPoint anyEndpoint = new IPEndPoint(IPAddress.Any, 9876);
try
{
client.Client.BeginReceiveMessageFrom(byts, 0, 8192, SocketFlags.None, ref anyEndpoint, ReceiveUDP, null);
}
catch (Exception beginReceiveError)
{
// Console.WriteLine("{0}", beginReceiveError);
}
}
static void Main(string[] args)
{
client = new UdpClient(new IPEndPoint(IPAddress.Any, 9876));
EndPoint anyEndpoint = new IPEndPoint(IPAddress.Any, 9876);
client.Client.BeginReceiveMessageFrom(byts, 0, 8192, SocketFlags.None, ref anyEndpoint, ReceiveUDP, null);
UdpClient server = new UdpClient();
server.Connect("172.20.10.4", 9876);
server.Send(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }, 8);
UdpClient server2 = new UdpClient();
server2.Connect("127.0.0.1", 9876);
server2.Send(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 9);
UdpClient server3 = new UdpClient();
server3.Connect("OOZGUL-NB02", 9876);
server3.Send(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 9);
client.Close();
server.Close();
server2.Close();
Console.ReadLine();
}
Output:
Received 8 bytes
from 172.20.10.4:59196
Receiving IP Address: 172.20.10.4
Network Interface #3
************************************
Received 9 bytes
from 127.0.0.1:59197
Receiving IP Address: 127.0.0.1
Network Interface #1
************************************
Received 9 bytes
from 172.20.10.4:59198
Receiving IP Address: 172.20.10.4
Network Interface #3
************************************

Oguz Ozgul
- 6,809
- 1
- 14
- 26
-
I think OP is asking for local address not peer address. I'm not familiar with C# and Async but according to your output those IP address seems to be the peer address not local address? – ourlord Jun 03 '20 at 19:26
-
@ourlord that is because both the client and server are my local machine. OP accepted this as answer. The IP addresses retrieved by this method will be the peers' – Oguz Ozgul Jun 04 '20 at 22:19