Tobias Langner
Tobias Langner

Reputation: 10828

How do I open an udp socket for a multicastgroup with Qt?

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.

Upvotes: 1

Views: 4578

Answers (2)

Indy9000
Indy9000

Reputation: 8881

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));
...

Upvotes: 3

ewanm89
ewanm89

Reputation: 929

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.

Upvotes: 0

Related Questions