Suppose I create a very simple socket connection, how can one programatically:
- Find out what interface (ip address / NIC) is being used.
- Force the other interface
Suppose I create a very simple socket connection, how can one programatically:
You will find these links usefull, since the question doesn't provide a language, here's for the most common ones.
So, use any of these links and go to the languge you are using. Find the function for getting your host address or ip address and use the Bind method for the appropriet language. This will force the application to bind to that interface.
a. After a connection is established, most socket APIs have a call for something like "getLocalAddress" that will tell you the interface that the OS selected for you.
b. Similarly, after a socket is created but before a connection is established, most socket APIs have a call to bind the socket to a local address and/or port.
Whatever language you are using, you will ultimately be using a sockets library layered on top of an implementation of the original BSD sockets C networking library. Although you may need to change some things for different libraries and languages, the following information should at least be helpful in targeting where to look in your library's documentation.
Normally, a server program issues the following sequence of calls:
socket()
returns a "floating" socket object.bind()
binds the socket to a particular well-known port number on (usually) all network interface cards (NICs) in the machine.listen()
causes the socket to go "live".accept()
blocks until a connection from a client machine is made.The point where you can decide which NIC to use is in the call to bind()
, which expects a data structure containing an IP address and a port number. The usual strategy is to pass the special value INADDR_ANY
for the IP address, indicating that connections should use any and all NICs in the machine, however a particular NIC's IP can be specified instead to receive connections only via that NIC.
Normally, a client program issues the following sequence of calls:
socket()
returns a "floating" socket object.connect()
binds the socket to a randomly selected port on a randomly selected NIC and attempts to connect to the remote host:port
combination specified.So how can a client choose what NIC to use? A client can also choose to call bind()
if it so desires, after socket()
and before connect()
. Usually this isn't done simply because connect()
will automatically bind an unbound socket in a way that enables access via any NIC (which is usually the desired behaviour), but this auto-binding can be turned off by calling bind()
explicitly. In this case, you should specify 0 for the port number to have the OS choose a random port number for you.
Once a connection has been made, you can call getsockname()
to get information about the socket, such as which IP it is bound to (i.e. which NIC it is communicating through) and what port number was assigned (in the case of a client program).