Sunday 23 September 2018

Socket Programming in Java

Networking / Socket Programming :
                      
A socket is one endpoint of a two way  communication link between two programs running  on a Network. A socket is a bi-directional communication between host. A computer on a network often termed as host.

Socket Dynamics : 


Socket is an abstraction of the network.Each end has an input stream( to send data) and output stream( to receive data) wired up to the other host.You store and retrieve data to and from network through socket, without actually going into underlying mechanism.

What is Port?


It is transport address to which processes can listen for connection request. There are different protocols available to communicate such as TCP and UDP. There are 64k ports available for TCP sockets and 64k ports for UDP.

Client-Server Communication: 

  • A server runs on a specific computer and has  a socket that is bound to a specific port number.
  • The server just waits, listening to the socket for a client to make a connection request. 
  • On the client side : The client knows the hostname of the machine on which the server is running and the port number to which the server is connected. 
  • As client creates  a socket that socket attempts to connect to the specified server. 
  • The server listens through a specific kind of socket, which is named as server socket.
  • The sole purpose of the server socket is to listen for incoming request; it is not used for communication. 
  • If everything goes well, the server accepts the connection. 'Upon acceptance, the server gets a new socket, a communication  socket , bound to a different port number.
  • The server needs a new socket so that it can continue to listen through the original server socket for connection requests while tending to the needs  of the connected client. The scheme is helpful when two or more clients try to connect to a server simultaneously. 
  • On the server side, if the connection is accepted, a socket is successfully created and the client can use the socket  to communicate with the server. 
  • Note that the socket on the client side is not bound to the port number used to make contact with the server. Rather, the client is assigned a port number  local to the machine on which the client is running.
  • The client and server can now communicate by writing to or reading from their socket. 
Steps - To Make a Simple Client: 
 To make a client, process can be split into 5 steps. These are:

1. Import required package: 
You have to import two packages 
  • java.net.*; 
  • java.io.*; 
2. Connect / Open a Socket with server: 
Create a client socket(Communication Socket)
     Socket s  = new Socket("serverName" , serverPort);
  • serverName: Name or address of the server you wanted to connect such as  http://www.google.com  or  172.2.4.98 etc. For testing if you are running client  and server on the same machine then you can specify "localhost" as the name of server.
  • serverPort: Port number you want to connect to 
The scheme is very similar to our home address and then phone number. 

3. Get I/O Streams of Socket: 
Get input & output streams connected to your socket. 
  • For reading data from socket 
          A socket has input stream attached to it. 
               InputStream is =  s.getInputStream(); 
               // now to convert byte oriented streams into character oriented buffered reader. 

              // we use intermediary stream that helps in achieving above stated  purpose. 
              InputStreamReader isr = new InputStreamReader(is); 
             BufferedReader br = new BufferedReader(isr);
  • For writing data to socket
         A socket has also output stream attached to it. Therefore, 
                OutputStream os = s.getOutputStream(); 
                // now to convert byte oriented stream into Character oriented Printer Writer
               // here we will not use any intermediary stream because PrintWriter constructor
              // directly accepts an Object of  OutputStream. 
              PrintWriter pw = new PrintWriter(os, true); 

Here notice that true is also passed to so that output buffer will flush. 

No comments:

Post a Comment

If you have any doubt please let me know

Methods in JAVA Language

 In general, a way may be thanks to perform some task. Similarly, the tactic in Java may be a collection of instructions that performs a sel...