2

I want to send data via a specific network interface

udpSocket->joinMulticastGroup(QHostAddress(Address),
                              QNetworkInterface::interfaceFromName(iface));

but, data is sending via another network interface. How can I force it to send data by that interface?

It is tested with SmartSniff.

Meysam Hit
  • 439
  • 1
  • 4
  • 13
  • I think you might have to look into calling `setsockopt` for the option [IP_MULTICAST_IF](http://www.tldp.org/HOWTO/Multicast-HOWTO-6.html) – simonc Jun 12 '13 at 11:15
  • @simonc : i used 'QUdpSocket Class' , i cant use setsockopt – Meysam Hit Jun 12 '13 at 11:19
  • 1
    Perhaps you're looking for [void QUdpSocket::setMulticastInterface(const QNetworkInterface & iface)](http://qt-project.org/doc/qt-5.0/qtnetwork/qudpsocket.html#setMulticastInterface)? – thuga Jun 12 '13 at 12:03
  • @thuga : i used this function , but not working correctly . – Meysam Hit Jun 12 '13 at 12:11
  • @MeysamHit Can you explain what is wrong? – thuga Jun 12 '13 at 12:22
  • @thuga : Problem : i want to send data over eth0 , but data send over eth1 – Meysam Hit Jun 12 '13 at 12:33
  • @MeysamHit Does `QUdpSocket::multicastInterface()` return the correct interface? – thuga Jun 12 '13 at 13:11
  • @thuga : yes udpSocket->multicastInterface().humanReadableName() return true name . – Meysam Hit Jun 12 '13 at 13:20
  • @MeysamHit `QUdpSocket::setMulticastInterface(const QNetworkInterface & iface)` only works when you bind your socket to an address. Maybe [this](http://stackoverflow.com/questions/6367209/using-qudpsocket-to-send-datagrams) will help you. – thuga Jun 12 '13 at 13:29

1 Answers1

2

Joining multicast groups is for receiving.

If you want your send() invocations to go on a specific interface you need to bind() to it.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • How can I bind my socket to a specific interface and force my packets to go from it? My problem is _sending_. – Meysam Hit Jun 13 '13 at 10:09
  • With the `bind()` method, as I said. – user207421 Jun 13 '13 at 10:16
  • I'm a bit confused. Can you show me an example? _I'm trying to send data, but I cant understand how `bind()` will help me to force the program to route data via a specific interface._ – Meysam Hit Jun 13 '13 at 11:57
  • You want to bind to a specific interface, so call `bind()`. Nothing confusing about that. Binding to a specific IP address will force all sends via that socket to be sent via the interface that has that IP address. – user207421 Jun 19 '13 at 11:20
  • Downvote because bind() only works for receiving under Windows. – Jorge Fuentes González Jan 04 '15 at 16:21
  • @JorgeFuentesGonzález Could you please elaborate on that? – user207421 Jan 04 '15 at 23:01
  • @EJP Now I hate myself. 3 weeks ago I was going to start writing a program that sends packets though different interfaces, so started gathering information about this, but I don't know how I got, here, in StackOverflow, that bind() on Windows only works for incoming data, so I connected 2 laptops, one with Windows and the other one with Linux to work on my project (obviously the Linux one is to divide the packets). Now I'm trying to find that info and I only read how to select interface before connecting under Windows... What the h**l have I searched the last time to read that...? – Jorge Fuentes González Jan 04 '15 at 23:49
  • Ok, I found it here (Chrome history...): http://stackoverflow.com/questions/2065495/using-a-specific-network-interface-for-a-socket-in-windows/2080495#2080495 and now reading more I see that that was in Windows OS under Vista version. Now works properly... I guess that I have to read more before taking a decision :-/ EDIT: Vote locked until answer is edited? Well, I guess that you can add that info telling that old OS do not work the normal way. – Jorge Fuentes González Jan 04 '15 at 23:50
  • @JorgeFuentesGonzález Edited it. As a general rule, if you disagree with an answer you should provide a reason, and preferable a counter-citation. [MSDN](http://msdn.microsoft.com/en-us/library/windows/desktop/ms737550%28v=vs.85%29.aspx) says 'The bind function might be used before send operations using the sendto,WSASendMsg, or WSASendTo functions if an application wanted to select a specific local IP address'. – user207421 Jan 05 '15 at 06:56