In the last tutorial we learn about  the socket declaration and the closing of socket by shutdown and close function. In this tutorial we read about bind function, listen and accept.

Up to now you have declared a socket that, it will be of this domain IPv4 or IPv6 and will use types SOCK_STREAM or SOCK_DGRAM or something else. Next step is to bind it with some IP address and Port, otherwise it will be of no use if address is not associated with it. And we need to have Address for both client and server, i hope we don’t have any doubt about that.

The address associated with the client socket is of little interest to us and we can let system choose the default address for us. for server, however we need to associate a well known address with the server socket on which client request will arrive.

Client need to discover a way to find out the address to contact the server and the simplest is for a server to reserve an address and register it in /etc/services or with name server.

Bind function is used to associate a address to a socket

syntax : socket.bind((HOST, PORT))

There are some restriction on the address which we can use :-

1) The address we specify must be valid for a machine on which the process is running, we cannot specify any address belonging to some other machine.

2) The address must match the format supported by the address family we used to create the socket.

3) The port number in the address cannot be less then 1024, unless the process have appropriate privilege.

4) usually only one socket end point can be bound to an address, although some protocol allow duplicate binding.

Once you have bind the socket to a particular IP Address we need to allow it to listen, so our next function is listen. In this tutorial we will just focus on server side, in next tutorial i will include function which are used in client.

Listen function is used when a server is willing to accept connect request from client.

syntax : socket.listen(backlock)

The backlock argument specifies the maximum number of queued connection. It should be at least 0(zero) and maximum value is system dependent (usually 5).

Once the queue is full the system will reject additional connect request, so the backlog value must be chosen based on the expected load of the server and the amount of processing it must do to accept a connect request and start the service.

Now once the server has called the listen, the socket used can receive a connect request. We use the accept function to retrieve a connect request and convert that into a connection.

syntax : socket.accept(connection, address)

Accept a connection. The socket must be bound to an address and listening for a connection the return value is a pair of connection and address. where connection is a new socket object used to receive and send data. And address is the address bound to the socket on the other end of the connection(client address).

You have to wait till tutorial 5 for the example of all these function. In next tutorial we will look about the client functions.

Follow us on Facebook.

Thanks
Abhishek.

Comments
  1. Would you kindly send me additional material regarding this post?
    Even just several related links you find helpful will be great, thanks a
    lot in advance! 🙂

  2. What is the quickest way to touch base with the individual who published
    this post? I know it maybe a lot to ask, but is it possible to arrange a phone meeting just for Five minutes?

    • Networls says:

      Hello..sorry for late reply..please tell me in brief what exactly you want to know in this topic…
      And the book which I refer is foundation of python network programming and advance unix programming….

Leave a comment