My question is, if machine A have two IP address X,Y.
Can it open port 80 twice,like X:80
and Y:80
?
Say,is port unique by machine or by IP?
My question is, if machine A have two IP address X,Y.
Can it open port 80 twice,like X:80
and Y:80
?
Say,is port unique by machine or by IP?
It's unique by IP. When you bind
, (that's the important part), you bind to an IP and a port number, not a machine and a port number. To bind to all addreses you can use something like INADDR_ANY
.
If you want to bind only to a few addresses, you have to do so "by hand". When the OS receives a packet it first checks he is the destination. Then it forwards it to the program that has requested
(through bind, through connect etc) that he be the destination of packets with that specific IP and port number.
port and IP's have 1 to 1 mapping.
So, yes you could have port 80 open on two different IP's on the same machine.
An IP address specifies a network interface (think an ethernet port on your computer or your WiFi connection). A port number specifies the process to which to route messages arriving on a given network interface. Hence you can use the same port number with different IP addresses, as they specify the port on which to listen on that given interface. Note, though, that you can even reuse a port number with the same IP address if you use the SO_REUSEADDR option when invoking the bind function.