Search code examples
cudpbind

Is bind() necessary if I want to receive data from a server using UDP?


Is it necessary to have in the client bind, if I want to receive data from a server?

I've seen a schema (look at the image below), and it seems that it's not necessary. But from what I know, bind is necessary to give an address to a socket. If I don't "bind" the socket, how can I send data to it?

UDP client/server


Solution

  • As a client, the application needs to be aware of the server port to which it needs to connect, and as a server, the application needs to be aware of the server port to which it needs to listen. Therefore, the server needs to bind to an IP address and port so that the client can connect to it.

    The client can simply create a socket and call connect(). Binding to an available IP address and ephemeral port shall implicitly happen. But, yes, nothing prevents you from having the client bind to a particular IP address and port.

    In the case of UDP, despite there being no "connection" mechanism, servers still need to bind to IP addresses and ports to allow the clients to send data to them.