NOTE: If you are aware of Unix Sockets then you can leave introduction part

What is a socket?

Just another bit of computer jargon? Going back a little into networking history, it is a Berkeley UNIX mechanism of creating a virtual duplex connection between processes. This was later ported on to every known OS enabling communication between systems across geographical location running on different OS software. If not for the socket, most of the network communication between systems would never ever have happened.
Taking a closer look; a typical computer system on a network receives and sends information as desired by the various applications running on it. This information is routed to the system, since a unique IP address is designated to it. On the system, this information is given to the relevant applications which listen on different ports. For example a net browser listens on port 80 for information. Also we can
write applications which listen and send information on a specific port number.
For now, let’s sum up that a socket is an IP address and a port, enabling connection.
To explain the socket we will take an example of Client – Server Programming. To complete a client server architecture we would have to go through the following steps

Creating A Server

  • Create a socket with socket call.
  • Bind the socket to a port address with bind call.
  • Listen to the socket at the port address with listen call.
  • Accept client connections with accept call.

Creating A Client

  • Create a socket with socket call.
  • Connect (the socket) to the remote machine with connect call.

Create a socket

The first step in establishing a network connection is creating a socket, with the sockt() function
socket creates a SOCKET. The other three arguments are integers which should have the following values for TCP/IP connections.
  • DOMAIN should be PF_INET. It’s probable 2 on your computer.
  • TYPE should be SOCK_STREAM for TCP/IP connection.
  • PROTOCOL should be (getprotobyname('tcp'))[2]. It is the particular protocol such as TCP to be spoken over the socket.
So socket function call will be something like this:


Bind to a socket address

The sockets created by socket are innocent and they have not yet been polluted by a computer hostname or a port number. Next socket function bind() fleshes out the socket with these values. Server uses bind() function to specify the port at which they will be accepting connections from the clients.

ADDRESS is a socket address which ( for TCP/IP ) is a packet string containing three elements
  1. The address family (For TCP/IP, that’s AF_INET, probably 2 on your system )
  2. The port number ( for example 21 )
  3. The internet address of the computer ( for example 10.12.12.168 )
As the bind() is used by a server which does not need to know its own address so the argument list looks like this:
The or die clause is very important – if a server dies without outstanding connections the port won’t be immediately reusable unless you use the option SO_REUSEADDR using setsockopt()function. Here pack() function is being used to pack all the data into binary format.

Listening on the port

If this is a server program then it is required to issue a call to listen() on the specified port.
The above call is mandatory for all the servers and here QUEUESIZE is the maximum number of outstanding connection request allowed. Generally, listen() is used in an infinite loop. As soon as one connection arrives the server deals with it and then goes back to listen for more connections.

Accepting connections

If this is a server program then it is required to issue a call to access() function to accept the incoming connections.
The accept call receive SOCKET descriptor returned by socket() function. Upon successful completion of this call, a new socket descriptor is returned. All future communication between client and server then takes place over NEW_SOCKET and SOCKET returns to what it does best : listen()ing for a new connection. If access() call fails then it returns FLASE which is defined in Socket module which we have used initially.
You will often see accept() used inside a while loop as follows
Now all the calls related to server are over and let us see a call which will be required by the client

Connection Establishment

If you are going to prepare client program then after using socket() call you would have to use another call connect() to connect to the server.

Here ADDRESS is a socket address similar to bind call, except that it contains the IP address of the remote server.

If you connect to the server successfully then you can start sending your commands to the server using SOCKET descriptor.
So now lets put all the things together

Script to Create a Server

Now starting above Server

To run the server in background mode issue the following command on Unix prompt

Script to Create a Client

What Do You Think on This ? Say Here