Question

I have a question about Qt & network sockets. If I have a computer with multiple IP-Adresses in different networks, how do I open an udp socket for a multicastgroup on a specific network-adapter/ip adress.

eg: ip 192.168.2.1 and 172.20.0.1 and I want to create a socket that receives packets from the multicast group 228.5.6.7 on the 172.20.0.1 network adapter.

Was it helpful?

Solution

You should set that in imr_interface as shown below: (probably it's set to INADDR_ANY now)

struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr("228.5.6.7");
mreq.imr_interface.s_addr = inet_addr("172.20.0.1");// <---- right here
...
QSocketDevice* sdev = new QSocketDevice(QSocketDevice::Datagram);
...
setsockopt(sdev->socket(), IPPROTO_IP, IP_ADD_MEMBERSHIP,(const char *)&mreq, sizeof(struct ip_mreq));
...

OTHER TIPS

If it's a listening socket, you can use bind to IP address to bind it to a specific IP address to listen on. If it's a client socket, the OS manage the right interface to create it on to reach that IP address as per routing table rules.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top