Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can multiple sockets be associated with same port for UDP?

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

But can the same thing work for UDP?

like image 417
Terminal User Avatar asked May 27 '11 05:05

Terminal User


People also ask

Can multiple sockets bind to same port?

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.

Can UDP send and receive on same port?

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.

Can multiple clients send data to the same UDP socket used by a server?

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.

Can UDP handle multiple clients?

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.


2 Answers

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

like image 99
caf Avatar answered Oct 14 '22 10:10

caf


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.

like image 37
user207421 Avatar answered Oct 14 '22 11:10

user207421