Sunday 29 January 2012

Server side socket tutorial ..made simple and easy !!!

Look into the client side first


In the server side the operation is no different but it involves certain other stuff which should be taken care of
We begin ,yes with the creation of socket

sid= socket(AF_INET, SOCK_STREAM, 0);

but before proceeding lets look at some more of the program
 
struct sockaddr_in saddr, caddr;

this creates a structure some thing like a drawer where each entity has its place and also different type can be stored something like you can keep books and clothes in different section of the drawer so that you can later access them pretty easily..structure does just that
This structures are defined beforehand and we need not worry at all

saddr.sin_family=AF_INET;
    saddr.sin_addr.s_addr=htonl(INADDR_ANY);
    saddr.sin_port=htons(5122);

The 1st line just mentions the type of address format,and INADDR_ANY is a way of obtaining the ip address of the hostmachine in which the server runs,so if you use it in your machine ,you should get your ip address.
And the third one mentions the port number ..we randomly select 5122.You can go ahead and take what you like 22253 or 11489 or 86542 etc

once the socket is in place we need to bind it to the address which we have worked so far
we do so by :  bind(sfd, (struct sockaddr_in *)&saddr, sizeof(saddr));

now this socket represents the address and this address leads to a service because we have a port number as well in the address..

The server is ready to listen to connection from the clients we can ask the server to listen to client requests by using :  listen(sid, 5);
Please observe the socket here is represented by the id sid and the 5 indicates the queue length which can store the requests or if more than 1 request to the server occur simultaneously then a queue of request can be maintained.
Now we have dedicated this socket as a connection listener all it does is look out for connection requests something like a receptionist ,a pretty receptionist if i might add !

The server can accept the connection by :
cid=accept(sid, (struct sockaddr *)&caddr, &len);

Here actually a new socket is created so we don't disturb the receptionist who can always look for client request,with this new socket we can directly communicate with the client.The caddr stuff will give out the client address and the server need not know the client existence before this,makes sense eh!

then with that we can use read write operation and life becomes simpler.
THE END

Hold on! what will happen if multiple clients request for the same service in the server ?
We have seen the use of a queue in the listen ,but its only for listening and that too maximum queue length is 5.So our receptionist can handle 5 one after the other at a given time.
So all these people around the world use google and facebook ,how is it possible ?
well we use parallel processing we can use fork() which is done here to create a new virtual server something like this: The server sees more clients are needed their service so it clones itself and each of the clones will handle a single client
Problem solved!

So in short in server side

1.make a socket
2.bind it with the server ip address and a port number all the client should know this address to access the server
3.use this to listen to requests
4.for each request create a server clone and use a different socket to communicate with the client
5.use the read ,write to talk


Thats about it!

No comments:

Post a Comment