.NET Framework Developer Center >
.NET Development Forums
>
.NET Framework Networking and Communication
>
How to receive UDP packets from clients with any IP address (from any subnets)
How to receive UDP packets from clients with any IP address (from any subnets)
- Hello everybody,
I'm programming an application in C# which is communicating with small devices, on Ethernet, using UDP.
The aim is that all my devices could have different IP addresses, not even in the same subnets, and that I could discover them using my C# application.
I programmed it using a socket as below:
IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0); _remoteDrive = (EndPoint)remote; _broadCastIpEndPoint = new IPEndPoint(IPAddress.Broadcast, NetXConstants.NetXCommPort); ... _socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); if (_networkInterface != null) { IPEndPoint localIPEndPoint = new IPEndPoint(_networkInterface.IPAddress, 0); _socket.Bind((EndPoint)localIPEndPoint); } _socket.ReceiveTimeout = UDP_BROADCAST_TIMEOUT; _socket.EnableBroadcast = true; ... _socket.SendTo(bytesToSend.ToArray(), _broadCastIpEndPoint); ... status = _socket.ReceiveFrom(dataReceived, SocketFlags.OutOfBand, ref _remoteDrive);
My problem, as the title of this thread says, is that I only receive packet from the device which are in the same subnet as I am.
For the other devices, I see the answered packets in WireShark, but the call "ReceiveFrom" doesn't get them and finish with a timeout.
Can anybody help? I'm stuck with this for hours now... and still googling...
Thank you in advance,
Antoine
All Replies
- I'm not a UDP person, but it seems to me that you're only getting the packets from your local subnet because your socket is bound to a local IP address.
Try calling Bind with an IPEndPoint.Address of IPAddress.Any instead of _networkInterface.IPAddress.
-Steve
Programming blog: http://nitoprograms.blogspot.com/
Including my TCP/IP .NET Sockets FAQ
Microsoft Certified Professional Developer - Thank you steve, but I tried that already and it doesn't work,
- Anyone else has an advice?
<br/> I suggest you to do a direct broadcast on each interface<br/> <br/> <br/> static private IPAddress[] GetDirectBroadcastAddresses() { IDictionary<string, IPAddress> tmp = new Dictionary<string, IPAddress>(); NetworkInterface[] netwiList = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface netwi in netwiList) { if (netwi.OperationalStatus == OperationalStatus.Up) { IPInterfaceProperties prop = netwi.GetIPProperties(); if (prop!=null) { foreach (UnicastIPAddressInformation unicastAddress in prop.UnicastAddresses) { if ((unicastAddress.IPv4Mask != null) && (unicastAddress.Address != null)) //ca peut arriver { byte[] res = new byte[4]; for (int i = 0; i < 4; i++) { res[i] = (byte)(unicastAddress.Address.GetAddressBytes()[i] | (unicastAddress.IPv4Mask.GetAddressBytes()[i] ^ byte.MaxValue)); } IPAddress add = new IPAddress(res); if ((!add.Equals(IPAddress.Broadcast) && (!tmp.ContainsKey(add.ToString())))) tmp.Add(add.ToString(), add); } } } } } IPAddress[] ret = new IPAddress[tmp.Count]; tmp.Values.CopyTo(ret,0); return ret; }
- Remember that UDP isnt a guaranteed message delivery. If you have computers in different subnets, the intermediate routers (and even the receiving host itself) could drop the packets.Also, what is the network topology? Are the routers configured to drop UDP broadcast packets? That may also be what is happening here.
feroze
--
My blog
Instruction on how to create a tracelog with your System.Net application
- To make sure you have a rock-solid setup for recieving UDP packets from any IP address you'll need:
-To create your socket or UdpClient and bind it to a local NIC on the port you want to recieve from.
-For different network pools, you'll need to configure the appropriate firewalls/NATs to forward UDP packets to the IP address of the machine you are listening on.
Some router firewalls/NATs have different setup features. Most use a system of defining the external port, internal port and a SINGLE ip address to forward the packets to. The reason for this is because the system allows the flexibility of setting up port mappings to be simplistic for external clients/devices. Some routers don't allow you to specify the target IP or even chose the protocol (UDP/TCP).


