Quantcast
Channel: Active questions tagged 22.04 - Ask Ubuntu
Viewing all articles
Browse latest Browse all 4421

Cannot receive UDP datagrams if I use an address OTHER than 0.0.0.0: on second Ethernet interface on my computer board

$
0
0

I'm running Ubuntu 22.04 on a board with two wired Ethernet interfaces (call them LAN1 (192.168.1.101) and LAN2 (192.168.2.1)). Newbie to developing for Linux, and I'm using .NET 6 runtime for my execution environment. That stuff works, or at least seems to. There's no WLAN/WiFi involved with this.

I want to use LAN1 to connect to my home network, getting access to Internet, etc. I have some devices that spit telemetry data out using UDP broadcasts. The intent is to have that traffic hitting LAN2, and the receiving application can then grab the data, munge it, and eventually spit something out via LAN1.

The one thing I'm trying to do is only listen for and deal with to UDP traffic on the LAN2 interface. If something comes in on the LAN1 interface, I don't want to know about it.

Looked up how to create a simple UDP listener and have two versions of test code. One uses UdpClient; the other is using sockets. This is all basic Microsoft Learn/CodeProject sort of test programs. Created code in VS 2022 targeting net6.0.

Basically, the sample code shows creating an IPAddress that's basically 0.0.0.0, tack on my port, and create and listen. Handle the bytes that come in. Further investigation (i.e. Google) on restricting work to just the one interface says to create the IPAddress using 192.168.2.1 (i.e. static IP of the interface I'm interested in), tack on the port, and create and listen.

The main bit of the test code is this:

_listenEP = new IPEndPoint(IPAddress.Parse("192.168.2.1"), _port);_listener = new UdpClient(_listenEP);_groupEP = new IPEndPoint(IPAddress.Any, _port);while (true){    Console.WriteLine($"{Environment.NewLine}Waiting for broadcast  {DateTime.Now}");    byte[] bytes = _listener.Receive(ref _groupEP);    Console.WriteLine($"Received broadcast from {_groupEP} :");    ProcessData(bytes);}

If the first IP address is 0.0.0.0, then I see traffic and will handle it, but from either interface.

Results: If I run the code on my Windows 11 laptop with multiple wired interfaces, I seem to be able to do what I want. If I start with the 0.0.0.0, then I see UDP datagrams come in on multiple interfaces (via my handling code and by watching network traffic with Wireshark). If I restrict to a single IP address, then my datagram handling routine only fires for traffic on that specific interface. Great.

BUT ... if I then move that same code to my Ubuntu board and run it, everything works for 0.0.0.0 (but across both LAN1 and LAN2). If I start my routine to bind to 192.168.2.1 (LAN2), then I see NO traffic - no handling occurs, but Wireshark does show that the UDP datagrams are present on that interface.

SO ... Can anyone suggest something that I've overlooked for this "tie my program to one interface"? Some sort of network routing or configuration that's important for only hearing UDP traffic on the specific interface?

Thanks.


Viewing all articles
Browse latest Browse all 4421

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>