PlushBeaver
PlushBeaver

Reputation: 356

Why exactly binding a socket to multiple ports is not allowed?

Why does this limitation exist? What is the technical reason for it?

AFAIU, ports were introduced to distinguish between facilities (services, connections, etc.) of the same host, so logically the limitation is reasonable. However, SO_REUSEADDR exists to allow one-port-to-many-sockets binding, but not the other way round. It seems practical, because it would spare a system call wasted on multiplexing; many SO questions seek (fruitlessly) a way to do it. But the lack of implementation suggests there are some obstacles I cannot figure.

Upvotes: 3

Views: 2363

Answers (1)

David Hoelzer
David Hoelzer

Reputation: 16331

The reason is that UDP and TCP connections are keyed based on the IP-Port Pair. This is how the stack figures out what goes with what internally.

If we had many ports to one it would require some other mechanism to key the connection so that the proper data would be delivered to the proper application thread/session.

Upvotes: 5

Related Questions