Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A machine has 2 NICs. Which one will be utilized when connecting to a socket?

Tags:

sockets

Suppose I create a very simple socket connection, how can one programatically:

  1. Find out what interface (ip address / NIC) is being used.
  2. Force the other interface
like image 595
Bogdan Gavril MSFT Avatar asked Jan 09 '09 14:01

Bogdan Gavril MSFT


People also ask

Why do servers have 2 NICs?

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 activate more than one NIC?

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.

Can I have two NICs on the same subnet?

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.


1 Answers

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.

Servers

Normally, a server program issues the following sequence of calls:

  1. socket() returns a "floating" socket object.
  2. bind() binds the socket to a particular well-known port number on (usually) all network interface cards (NICs) in the machine.
  3. listen() causes the socket to go "live".
  4. 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.

Clients

Normally, a client program issues the following sequence of calls:

  1. socket() returns a "floating" socket object.
  2. 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).

like image 104
j_random_hacker Avatar answered Oct 19 '22 21:10

j_random_hacker