Can multiple sockets be associated with same port

2020-03-01 11:38发布

问题:

I think multiple sockets can be associated with same TCP port.

But can the same thing work for UDP?

回答1:

The only way to associate multiple sockets with a port in TCP is by listening and then accepting.

The purpose in that case is to give every incoming client a unique socket so as to keep their byte streams separate.

You don't need that in the case of UDP because there are no byte streams. You can write an entire UDP server using a single UDP socket. You just read, despatch to a handler for that client, the handler writes the response back via the same socket.



回答2:

Yes, it is also possible to have multiple sockets using a single UDP port.