Answer: Bind() function in socket programming is used to associate the socket with local address i.e. IP Address, port and address family.
int bind(int sockfd,struct sockaddr *servaddr,int addrlength);
There could be many combination of IP address and ports e.g. (10.17.18.19, 6400), (10.17.18.19, 6500), (10.17.18.19, 6600) and so on, a server can use.
Let’s say we have 3 servers A, B and C on the same machine. And a client wants to connect to server B. what is the identification of server B? How would we connect to it?
So, to provide an identification to a server, Bind () function associate sockets with IP address and port. Now, if we bind socket with address e.g. (10.17.18.19, 6500) for server B, a client can connect to this particular server specifying the same IP address and port.
//Create a socket i.e for UDP server
//Fill the address structure
servaddr.sin_family = AF_INET; //address family.
//Bind above socket and address
bind(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
- Since TCP/UDP server continuously wait and listen to incoming clients, it requires an identity, hence use bind() functions. Clients only need to know the address of server, that’s why they don’t use bind(), however you use bind() with clients there is no harm but useless excluding some cases if any.
- If we use INADDR_ANY e.g. servaddr.sin_addr.s_addr=htonl(INADDR_ANY), we are binding to all valid IPs the machine has. So it doesn’t matter what IP, a client is using to connect to server, it will work. For example if you have Wi-Fi and Ehernet on your machine, there should be 2 IPs.