Socket Programming

Anyone who has done some kind of network programming will be aware of the concept of sockets.
For those,who haven’t ever done it,this may be a very good place to start.

Me and 2 of my friends recently created a chat-server application using sockets in C in linux environment.What happened was,that once server-client connection was established,when a client would send some data to the server,the server would send the data to all the other clients.It was a localhost appplication however,and the primary purpose was interprocess communication implemented as a chat application.

So,a basic question,what is a socket?

A socket is a file descriptor–informally speaking,it is a way to connect between 2 processes,it can be thought of as a pipe extending from 1 process which carries data to some other process.The data is sent as a stream of bytes to and fro from 1 process to another and from the 2nd process to the 1st one.Each socket has associated with it an address structure which gives information to the socket functions about the position of the destination socket or position of the host socket.

Now,it may seem,that all this is very hard to implement.Actually,it is quite easy to implement sockets.UNIX provides libraries for this very purpose.These libraries contain functions that help to manipulate sockets so that we can exploit usage out of them.

Some common functions available in UNIX

1)socket()—this function takes 3 parameters–address family,type and protocol associated and returns socket descriptor value which we can save in a variable and use later to manipulate sockets.

2)connect()–This function is used to connect from a socket at host to the destination process.This function takes in 3 parameters–the socket descriptor returned above,reference to socket address structure sent as a pointer,and length of the address structure.

3)bind()–this function is used to bind the socket to an address structure that mentions about the port of the host process on which we are planning to listen for connections.This fucntion takes in 3 parameters–socket descriptor,reference to the socket address structure and again the length of the address structure.

4)listen()–This function is used to listen for new connections on a process.It contains 2 parameters,socket descriptor,i.e. the socket on which we are to listen for connections.Here,it is almost imperative to use bind() first before using listen()–because we need to listen on some particular port.And that can only be done when we use bind() first.

5)select()–this is used to maintain many different socket processes on a computer.It can deal with a  lot of sockets.It can be used to determine the state of a socket at a particular time,manipulate it and so on.

6)accept()–when we listen for connections and when we get a new function we have to accept() it if it is acceptable.This step is imperative prior to transfer of data.

7)Lastly,the important functions are read() and write() funtions—These are I/O functions that allow transfer of data to and from a process to another.There are many more ways of I/O,but read() and write() suffice at a beginner level.These functions send data as a stream of bytes.

Now to create a client the following functions were called in the respective order as mentioned below—-\

//Now I/O can be performed.

To create server following was used—-

//Now I/O can be done using select() to manipulate sockets and read()/write() in a loop

The flowchart given below can serve as a good tool to understand the functions used above


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s