Suppose I create a very simple socket connection, how can one programatically:
Separation of traffic (i.e. you could have a combo web/database server, same network, put all web traffic on one NIC, db traffic on the other, makes it easier to calculate loads for traffic types). This also makes it easier to split the two later on, nobody has to change connection strings. Save this answer.
Why would you want to have multiple NICs? Typically, this is a requirement for Network Appliances and for VMs passing traffic from one subnet to another. Having multiple NICs enables more control, such as better traffic isolation.
Configuring two or more NICs on the same subnet may cause communication problems. One of the most common scenarios resulting in multiple NICs being assigned to the same subnet is when both a wired and wireless interface are used to connect to the same network.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With