Sockets – socket connect() vs bind()

cnetwork-programmingsockets

Both connect() and bind() system calls 'associate' the socket file descriptor to an address (typically an ip/port combination). Their prototypes are like:-

int connect(int sockfd, const struct sockaddr *addr,
               socklen_t addrlen);

and

int bind(int sockfd, const struct sockaddr *addr,
            socklen_t addrlen);

What is the exact difference between 2 calls? When should one use connect() and when bind()?

Specifically, in some sample server client codes, found that client is using connect() and server is using the bind() call. Reason was not fully clear to me.

Best Answer

To make understanding better , lets find out where exactly bind and connect comes into picture,

Further to positioning of two calls , as clarified by Sourav,

bind() associates the socket with its local address [that's why server side binds, so that clients can use that address to connect to server.] connect() is used to connect to a remote [server] address, that's why is client side, connect [read as: connect to server] is used.

We cannot use them interchangeably (even when we have client/server on same machine) because of specific roles and corresponding implementation.

I will further recommend to correlate these calls TCP/IP handshake .

enter image description here

So , who will send SYN here , it will be connect() . While bind() is used for defining the communication end point.

Hope this helps!!

Related Topic