Question

As far as I understand sockets are just datastructures just like files. Does that mean a differnt socket DS is created for different combinations IP, PORTNUMBER and TRANSPORT TYPE (TCP/UDP)?

I want to know if the below implementations are possible

--> Make a Socket listen on 2 different IPs but same port number so that when a client sends data to any of these IPs(but same port number), Server should be able to serve it. (Or can a port number be associated with 2 different IPs)

--> Make 2 process listen on same port number

Was it helpful?

Solution

Make a Socket listen on 2 different IPs but same port number so that when a client sends data to any of these IPs(but same port number), Server should be able to serve it. (Or can a port number be associated with 2 different IPs)

A socket cannot listen on 2+ explicit IP addresses. You can call bind() on a socket only once, and it does not allow you to specify more than 1 IP address at a time. However, you can bind() a socket to a wildcard IP address (INADDR_ANY for IPv4, IN6ADDR_ANY for IPv6), which will allow the socket to bind to all available local IP addresses at the same time. In that scenario, you can indeed send data to the same port on any IP address that the socket is bound to.

Make 2 process listen on same port number

This is only possible if each process binds to a different IP address and no wildcard IPs are used. If you had multiple processes listening on the same IP and Port, how would they know which process should process which data? They wouldn't, so the OS does not allow it in the first place.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top