Sockets – Bind in TCP/UDP Sockets

cnetwork-programmingsocketstcpudp

Bind function is used to assign a name (a sockaddr struct) to a socket descriptor. Why is it required for TCP server but not TCP client ? And why it is required for bot UDP Client and server?

I have also written correctly working code without using bind() in UDP Client .

I do not understand why bind() is not used universally i.e. in all cases above.

Best Answer

Binding is only a required, if there is no other way for the computer to know which program to send the packets to. For connection less programs this is only the receiving end.

Please have a look at socket connect() vs bind() this post.

There a much better job of explaining is done than I'm able to do. If you've got any questions after. Feel free to ask:)

Related Topic