Sockets – Why is bind() used in TCP? Why is it used only on server side and not in client side

bindnetwork-programmingnetworkingsocketstcp

I wanted to know the exact function of bind() in TCP. What does it mean by 'binding' a local address to the socket? If it's assigning a port number to the socket, then why don't we use it in the client? I know that port is assigned by OS automatically in the client side, but I'm not getting the big picture of how all of this works.

After bind(), we listen(). How is the bind related to listen()? Will the listen() know that bind() has been executed? If so, what changes does bind() make so that it is known? I mean, how does returning zero for successful execution help?

I've gone through many definitions, but no where I could get all of this in detail. So if anyone can please explain this to me, I will be grateful.

Best Answer

It assigns the "local" end's port number.

For a server socket, this is the ultimate way to go - it is exactly what is needed: have your socket be bound to port 80 for a web server, for example.

For a client socket, however, the local address and port is normally not of importance. So you don't bind(). If the server restricts its clients to maybe have a certain port number, or a port number out of a given range, you can use bind() on client side as well.

On the other hand, you might as well be able to listen() on a socket where you haven't called bind() (actually I'm not sure about that, but it would make sense). In this scenario, your server port would be random, and the server process would communicate its port via a different means to the client. Imagine a "double-connection" protocol such as FTP, where you have a control connection and a data connection. The port the data connection listens on is completely arbitrary, but must be communicated to the other side. So the "automatically determined port" is used and communicated.

One example in Python:

import socket
s = socket.socket() # create your socket
s.listen(10) # call listen without bind
s.getsockname() Which random port number did we get?
# here results in ('0.0.0.0', 61372)

s2 = socket.socket() # create client socket
s2.connect(('localhost', 61372)) # connect to the one above
s3, x = s.accept() # Python specific use; s3 is our connected socket on the server side
s2.getpeername()
# gives ('127.0.0.1', 61372)
s2.getsockname()
# gives ('127.0.0.1', 54663)
s3.getsockname()
# gives ('127.0.0.1', 61372), the same as s2.getpeername(), for symmetry
s3.getpeername()
#gives ('127.0.0.1', 54663), the same as s2.getsockname(), for symmetry
#test the connection
s2.send('hello')
print s3.recv(10)