Reputation: 245
I have a Linux computer with 2 Ethernet adapters. I also have 2 ADSL models and 2 internet connections. I connect modem A to Ethernet port A and modem B to Ethernet port B.
Now, how to do the following (preferably in C++):
a) Get the IP of each adapter
b) Select the connection to use for downloading (I want to say: download this file with connection A and this with B)
The IPs are dynamic. I am doing this, because my IP must be know to a remote server.
The server must:
a) get the IP
b) send files to this IP
The idea is, every time my IP changes, I will send the new IP to the server, so the server will know where to send the files.
I am using 2 internet connections for:
a) redundancy reasons (if one internet connection is down, I got the second)
b) have faster download speed by opening 2 connections with the server.
Upvotes: 1
Views: 122
Reputation: 104559
If your goal is to simply update a server of your IP address, then you just need to make a connection to this server using a typical TCP socket:
int sock = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in addrLocal = {};
result = connect(sock, (sockaddr*)&server_address, sizeof(server_address));
send(sock, "I have a new IP address", ...);
In the above example, you don't even have to call bind
on the socket, as the client TCP/IP stack will consult your computer's routing table for the best local IP address to use (and will pick a random local port).
The client need not even know the IP address it is connecting from nor does it need to tell the server via the socket protocol.. The server in turn can automatically detect your IP address when it does the corresponding accept call when it receives the client connection.
sockaddr_in addrRemote = {};
socklen_t addrRemoteSize = sizeof(addrRemote);
int sockclient = accept(listensocket, (sockaddr*)&addrRemote, &addrRemoteSize);
// the IP address of the client making the connection is in addrRemote.
And if your client just keeps the socket to the server open, then the server can transmit a file back to the client without having to establish a new connection or keep track of any IP address.
Now to answer your original questions, in case you do have a legitimate need to do ascertain a local IP address.
Question 1:
To get the local IP address of each adapter, you can call getifaddrs . From the result list returned from this function, filter out any address that isn't IP, is not UP, or is LOOPBACK.
Question 2:
To bind a socket to a specific adapter, bind to the IP address of the local adapter prior to making a connect call. Example below
...
result = bind(sock, (sockaddr*)&addr, sizeof(addr));
if (result != -1)
connect(sock, (sockaddr_in*)&remoteServer, sizeof(remoteServer));
Where "addr" in the code sample above points to one of the ifa_addr values in the ifaddr array returned by getifaddrs.
Now if there is a NAT involved in any of these connections, then your locally enumerated IP address will be different than the public IP address that the server sees you at.
If you are using UDP sockets, then all of the stuff above still applies, with some tweaks. (e.g. don't call connect(), just call sendto() ).
Upvotes: 2