How would you do that without using Qt?
How would you do that without using Qt?
You can determine which interface the operating might use by querying the system routing tables and looking for the least cost* route that would apply to your destination and protocol (IPv4, IPv6 or something more exotic). You could start at GetIpForwardTable() on Windows or the /proc/net/route pseudo-file on Linux (also see man route).
* for whatever metric the operating system concerned might apply
On a windows environment, I'd create a connection to the desired address, then do a 'GetTcpTable', find the MIB_TCPROW that corresponds to the connection, and get the dwLocalAddr from the row structure. Now, as I'd like to have an 'OS-agnostic' solution (thus the move of my app from MFC to QT), I hoped that QT might have some functionality which would allow me to do the same.
Added after 5 minutes:
I was really hoping not to have to 'parse' the routing table. In a way I was looking for something like a 'GetTcpTable' (see my other reply), where all the routing is already done by underlying OS. In the end though (if no proper QT solution can be found), I might have to write specific implementations of the function for all supported OS-es, which, of course, I'd really like to avoid if possible.
Last edited by Buldozer; 2nd July 2012 at 07:51.
Qt is an application development framework, not a swiss army knife.
Unfortunately /proc/net/route will not work on any system that is not using a static routing table. Moreover with bonded interfaces there is no answer to your question at all. And even custom firewalling rules might influence the final interface that is used to send/receive packets and there is nothing you can do about it.
Bookmarks