0

suppose a nic having two different IP address for each ethernet for e.g

eth0 having IP address if 1.2.3.4
eth1 having IP address if 5.6.7.8

can i open a connection with one port e.g 1234 for each ip address for example

 1.2.3.4 binds on 1234
and 
5.6.7.8 binds on 1234

or i should be getting the error the port is already be bind

i can do this one for IPv4 and one for IPv6, not sure whether different ip address and same port on the same machine will work or not

anish
  • 6,884
  • 13
  • 74
  • 140

2 Answers2

0

You don't have "a nic having two different IP address". You have two nics, each with an unique ip address. Under these circumstances it is right do open the same port (and bind) on each ip/nic.

0

A nic has two different ip? what is eth0, eth1? it shows two nics.

anyway, if your machine has two nics, your can bind. but your machine has one nic and two ip addresses, use INADDR_ANY.

struct sockaddr_in sin;
sin.sin_family = AF_NET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(1234);

bind( sock, ...., (struct sockaddr*)&sin, ... );

if an interfaces use IPv6 Address, socket domain must be AF_INET6.

int sock = socket( AF_INET6 , ... ,... );
user2990252
  • 181
  • 1
  • 1
  • 9