How do I receive UDP broadcast packets using GTK / GIO?

35 Views Asked by At

I have a custom product (running Linux) spitting out broadcast packets every few seconds to port 38280.

Here's a screen capture of Wireshark showing the packets in question.

Wireshark capture

Under a Linux VM (Ubuntu 22.04 guest, Win11 host), I can run netcat to see these broadcast packets:-

$ nc -lu 38280
<xmllink><hello address="0" id="C8A0308399A3" ident="13579"/></xmllink>

I'm now trying to write a simple GTK app to listen out for these packets.

So far, I have tried using g_socket_listener_add_inet_port() but (after some extra testing) this will only see the packets if I send the packets directly to the IP address & port (rather than a broadcast message).

g_socket_listener_add_inet_port((GSocketListener*)l_svcKnockKnock, l_nSlaveAnnouncePort, NULL, &error);

Can anyone explain what I'm missing ?

Is g_socket_listener_add_inet_port() only for TCP packets ?

If so, how can I setup a listener for UDP broadcasts ?

UPDATE

I've now found this SO posting which gave me some working code.

Note that it doesn't use a normal listener service ... instead it adds a watch to the socket using g_io_add_watch().

Not sure how portable this is but it works for me !!

1

There are 1 best solutions below

1
Jussi Kukkonen On

Is g_socket_listener_add_inet_port() only for TCP packets ?

Yes.

You should be able to handle UDP with the slightly lower level g_socket_listener_add_address().