Wednesday, March 30, 2016

Sockets : Finally they unraveled themselves : Part 1


Socket programming was something, I was always interested in. But Now when i look back in my college years , I realized that nothing was taught which could have been practical. The teaching situation in india is quite bad.

If internet would have not come, we would still be lagging behind a lot.

Also I am very lazy, until and unless there is no external force, my inertia of rest doesn't allow me to work.


But past is past...behold ..the future is bright.............

Rather than having detailed techincal discussion about them, we will see there overview and then see Android Programming.


1) OSI layer :  If anyone has ever taken any networking course, they know about OSI layer. Below is the diagram



Now , in the above diagram SOCKETS fall between Transport and Session Layer. 
Transport Layer has TCP/UDP.\
 Internet Layer has IP. 

To communicate between any two devices, we need IP address. But what is multiple applications on one device are communicating with other device.  In that case we need PORT, which helps in distinguish traffic flow. 

SOCKET are combination of IP address and PORT, (APPLICATION/Presentation/Session) Layer opens SOCKET with Transport Layer and then all data is read/write from this socket. 


So now we know what is socket and how transfer can be done. Lets see what are API available in Android/Java library to open,close,read,write from a SOCKET. 

Also most of data transfer happening on internet or most of the Android apps we see are based on Server-Client architecture. 

A client will periodic check with Server if some new data is available from him, and will post if any data needs to be posted by Client to Server. 

So if you visualize , Client should know Server IP address, but if Server do not know Client IP address , it will be fine, as onus is on Client and not server. 


So the life - cycle of SOCKET is like below 




1) You create a socket on SERVER using a PORT ID.  ( Note depending on language and platform you are working on , you might also have to BIND the socket to Server IP address)

2) Then SERVER is constantly listening to any NEW CONNECTION coming. 

3) At CLIENT side, we also create socket. NOTE : the use of client side socket is to communicate with SERVER, hence the IP address and PORT no which we provides here is of SERVER.

4) Then CLIENT tries to connect to SERVER. 

5) SEVER once gets CLIENT request, accepts it.

6) Now we have communication link open and data can be transferred between SERVER and CLIENT.

7) Finally we close the connection. 

Now a bit of  Operating System .  If you see SERVER functions design wise, SERVER has to listen to new CLIENT request and also needs to communicate with existing CLIENT, hence we have to need two different THREADS, since both of these functions need to work in parallel. 



Now in Android, we use 

1)  ServerSocket serverSocket  = new ServerSocket(port_no);

    // This is to create Server side socket 

2 and 5) Socket socket_c = serverSocket.accept();

    // This is use to accept client connection, now using socket_c we can communicate with Client, NOTE : we don't use serverSocket to communicate with client, that is only used to listen and accept client connections. Once client connection is accepted, further communication is done using the client socket created at server side for each client.


3 and 4)  socket = new Socket(Server IP address, Server Port);

   // This is used to create socket at client side. This only will also try to connect to server, no other function needs to be called explicitly.



6 ) InputStream inputStream = socket.getInputStream(); 

     // This is used to read data coming to socket, irrespective of socket at server or client side.

    OutputStream outputStream = socket.getOutputStream(); 
    PrintStream printStream = new PrintStream(outputStream);
    printStream.print("data to be send in string format");

    // Below commands are needed to write data into socket.


Viola,  we are almost there. 

So in next post we will see Android App code , as it will also involve thread. 



No comments:

Post a Comment