Question

What is the main purpose of bind and connect methods in a datagram socket? Are they necessary to use? Does the usage of these methods have any relation with the entries in a router for incoming and outgoing packets?

Was it helpful?

Solution

What is the main purpose of bind and connect methods in a datagram socket?

bind() binds the socket to a local interface and port. connect() conditions the local UDP stack to only send to and receive from the connect target, as far as that socket is concerned.

Are they necessary to use?

It is only necessary to use bind() if you need to use a specific local port so that peers who are expecting to send to that port can succeed. It is only necessary to use connect() if you are only interested in one peer and want all other UDP activity filtered out automatically.

Does the usage of these methods have any relation with the entries in a router for incoming and outgoing packets?

If you don't call bind(), the first time you send any data anywhere an automatic bind() will take place to the IP address indicated as the best local route to the target by the unicast IP routing tables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top