I think multiple sockets can be associated with same TCP port.
But can the same thing work for UDP?
No. Only one application can bind to a port at a time, and behavior if the bind is forced is indeterminate. With multicast sockets -- which sound like nowhere near what you want -- more than one application can bind to a port as long as SO_REUSEADDR is set in each socket's options.
TCP vs UDP Once connected, a TCP socket can only send and receive to/from the remote machine. This means that you'll need one TCP socket for each client in your application. UDP is not connection-based, you can send and receive to/from anyone at any time with the same socket.
You could create a new UDP socket, bind() it to a different port, and then instruct the client to start sending traffic to the new socket's port instead of the usual one; but note that there isn't really any advantage to be gained by doing that.
You simply send out datagram packets to each client in turn. The packets will have the same origination port and IP, but each client will have a different IP address and/or port number.
Yes, it is also possible to have multiple sockets using a single UDP port.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With