0
votes

Bind() is not required while sending the UDP message. Reason : We dont need to bind() explicitly as OS automatically binds the sender with an IP address and PORT number.

Why bind() is required to receive the UDP message ? Sender can get the IP address & PORT number of the receiver from the previous message received and send an UDP message using that right?

2
What happens for the exchange prior to the "previous" message? And before that? And before that? .... You have to start somewhere, and someone must have a definite address for the other side to connect to.Siyuan Ren

2 Answers

0
votes

Your question doesn't make sense. It isn't required if you've already send a message, because you're already bound. It's required if you haven't already sent a message, in which case the technique you describe cannot possibly apply.

0
votes

UDP is connectionless, but it is still based on bound ports. The receiver must call sendto(), connect(), or bind() to establish a local bound port so the OS knows which port to allow inbound data on, and to establish the port that the sender needs to send data to. If the receiver is the first party to send a packet, then the bind can be implicit, but if the receiver is not the first party then a bind must be done explicitly.

If the receiver did not bind a local port, the port would not be opened at worse, or would be randomly chosen by the OS at best. Either way, the sender would have no hope of knowing which port to send data to, unless the receiver told the sender through external means. Even then, in order for the receiver to discover a randomly assigned port, it would have to query the socket, but an unbound socket cannot be queried. So the receiver has to perform a local bind one way or the other.