socket connect() vs bind()
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.
6 Answers 6
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 .
So, who will send SYN here, it will be connect() . While bind() is used for defining the communication end point.
thanks bro. With the diagram everything can unstand fastly. Can you tell what is the difference here, If we are using udp?
The one liner : bind() to own address, connect() to remote address.
Quoting from the man page of bind()
bind() assigns the address specified by addr to the socket referred to by the file descriptor sockfd. addrlen specifies the size, in bytes, of the address structure pointed to by addr. Traditionally, this operation is called «assigning a name to a socket».
The connect() system call connects the socket referred to by the file descriptor sockfd to the address specified by addr.
- bind() associates the socket with its local address [that’s why server side bind s, 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.
@SiddharthaGhosh No. maybe client and server are on same machine, but still they are different process, right? Both the API servs its own puprpose. They are never interchangeable
I think it would help your comprehension if you think of connect() and listen() as counterparts, rather than connect() and bind() . The reason for this is that you can call or omit bind() before either, although it’s rarely a good idea to call it before connect() , or not to call it before listen() .
If it helps to think in terms of servers and clients, it is listen() which is the hallmark of the former, and connect() the latter. bind() can be found — or not found — on either.
If we assume our server and client are on different machines, it becomes easier to understand the various functions.
bind() acts locally, which is to say it binds the end of the connection on the machine on which it is called, to the requested address and assigns the requested port to you. It does that irrespective of whether that machine will be a client or a server. connect() initiates a connection to a server, which is to say it connects to the requested address and port on the server, from a client. That server will almost certainly have called bind() prior to listen() , in order for you to be able to know on which address and port to connect to it with using connect() .
If you don’t call bind() , a port and address will be implicitly assigned and bound on the local machine for you when you call either connect() (client) or listen() (server). However, that’s a side effect of both, not their purpose. A port assigned in this manner is ephemeral.
An important point here is that the client does not need to be bound, because clients connect to servers, and so the server will know the address and port of the client even though you are using an ephemeral port, rather than binding to something specific. On the other hand, although the server could call listen() without calling bind() , in that scenario they would need to discover their assigned ephemeral port, and communicate that to any client that it wants to connect to it.
I assume as you mention connect() you’re interested in TCP, but this also carries over to UDP, where not calling bind() before the first sendto() (UDP is connection-less) also causes a port and address to be implicitly assigned and bound. One function you cannot call without binding is recvfrom() , which will return an error, because without an assigned port and bound address, there is nothing to receive from (or too much, depending on how you interpret the absence of a binding).