tags:

views:

1094

answers:

2
+1  Q: 

QT & UDP-Socket

Hi,

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.

+3  A: 

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));
...
Indeera
I guess he is using the QT networking module and not raw operating system provided sockets (saves the slight differences in winsock if making cross platform app).
ewanm89
ewanm89: This is how you doit using Qt sockets. sdev is a QSocket device.QSocketDevice* sdev = new QSocketDevice(QSocketDevice::Datagram);
Indeera
A: 

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.

ewanm89
won't work on multicast adresses though - that's my problem.
Tobias Langner
See yuriy's post http://www.qtcentre.org/forum/archive/index.php/t-3347.htmlYeah, can't set the multicast option in the QT api directly yet.
ewanm89