Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 732
Crocode Avatar asked Oct 06 '12 19:10

Crocode


People also ask

Why bind is not used in client side?

It's because client always have to connect to the server.

Why do we need to call bind () in server code and not in the client code?

You only need to bind on the client if the server expects you to be coming from a specific port or port range. Some services only allow connections from port numbers less than 1024, those only bindable by the superuser, though that doesn't mean much these days now that everyone controls their own machine.

Can bind be used with client?

On the client side, you would only use bind if you want to use a specific client-side port, which is rare. Usually on the client, you specify the IP address and port of the server machine, and the OS will pick which port you will use.


2 Answers

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) 
like image 76
glglgl Avatar answered Sep 22 '22 15:09

glglgl


bind() defines the local port and interface address for the connection. connect() does an implicit bind("0.0.0.0", 0) if one has not been done previously (with zero being taken as "any").

For outgoing connections, this is generally acceptable and preferred. The OS will simply bind to "all interfaces" and pick some high-numbered, unused port. You only need to bind on the client if the server expects you to be coming from a specific port or port range. Some services only allow connections from port numbers less than 1024, those only bindable by the superuser, though that doesn't mean much these days now that everyone controls their own machine.

For incoming connections, you have to bind to a known port so clients know where to contact you. Once they do so, they've given the server their local address/port so that communication can then flow in both directions. listen() will only work after a bind() call.

All sockets must bind, whether they be UDP, TCP, or other. It's just not always explicitly done.

like image 32
Brian White Avatar answered Sep 23 '22 15:09

Brian White