Networking in JAVA

Introduction

 
A computer network is an interconnected collection of computers. The node of a network communicates others using a wired/wireless medium. This communication is actually carried between two processes residing in two different machines. The processes on one machine initiates the request and another responds to the request and another responds to the requests. The initiator is the client and the responder is the server
 

TCP Protocol Suite

 
The TCP/IP protocol suite contains a number of  protocols. Its a four layer model followed by the network s. The internet also follows the same model. The client and server applications created by the user reside at the Application layer. They interact with the transport layer using the socket API. Java provide a package java.net. The transport layer take the data from takes data from application layer (sender) and breaks up in to segment attached the port number used by application to the data and passes in to the network layer. Depending upon the protocols used.
 
TCP/IP models figure
 
TCPIP Diagram.jpg 
 

Socket Programming in Java

 
Network programming revolves around the concept of sockets. A socket is one end-point of a two-way communication link between two programs running on the network. In Java sockets are created with the help of Socket classes and these class are found in the java.net package. There are separate classes in the java.net package for creating TCP sockets and UDP sockets. The client socket is created with the help of the Socket class and the server socket is created using the ServerSocket class.
 
socket figure
 
Socket Programming in Java 
 
Syntax
 
// for tcp client side
Socket s= new Socket (InetAddress adr, int port);
 
//for server side
ServerSocket ss= new ServerSocket (InetAddress adr, int port)
 
Step 1 - Creating a Socket on the client side (code):
  1. // importing the usefull package like net and io  
  2. import java.net.*;  
  3. import java.io.*;  
  4. class MySocket {  
  5.     public static void main(String[] args) {  
  6.         try {  
  7.             // using socket class constructor create socket  
  8.             Socket s = new Socket(InetAddress.getLocalHost(), 15);  
  9.             System.out.println("your socket is created");  
  10.             // using method of socket class find the local address  
  11.             System.out.println("Local Address :" + s.getLocalAddress());  
  12.             // using method of socket class find the local host  
  13.             System.out.println("Local Host :" + InetAddress.getLocalHost());  
  14.             // using method of socket class find the local port number  
  15.             System.out.println("Local Port :" + s.getLocalPort());  
  16.             // using method of socket class find the local address   
  17.             System.out.println("Inet Address :" + s.getInetAddress());  
  18.             // creating input stream by help of method getInputStream()  
  19.             InputStream in = s.getInputStream();  
  20.             // Buffered reader object is created to read strings from the socket.The Byte Stream is converted to character stream using the      
  21.             InputStreamReader  
  22.             BufferedReader br = new BufferedReader(new InputStreamReader( in ));  
  23.             String str = null;  
  24.             while ((str = br.readLine()) != null)  
  25.                 System.out.println(str);  
  26.             in .close();  
  27.             s.close();  
  28.             // catch exception is printing  
  29.         } catch (Exception e)  
  30.         {  
  31.             System.out.println(e);  
  32.         }  
  33.     }  
  34. }  
Step 2 - Creating a TCP Server (code):
  1. import java.io.*;  
  2. import java.net.*;  
  3. import java.util.*;  
  4. class MyServerSocket  
  5.   {  
  6.    public static void main(String[] args)   
  7.       {  
  8.          try {  
  9.               ServerSocket ss=new ServerSocket(15);  
  10.                while(true)  
  11.                      {  
  12.                        Socket client =ss.accept();  
  13.                        System.out.println("socket is created");  
  14.                    // using method of ServerSocket class find the client  address  
  15.                      System.out.println("client inet Address :"+client.getInetAddres());  
  16.                     // using method of ServerSocket class find the client port number  
  17.                      System.out.println("client Port number :"+client.getPort() );  
  18.                      // getting the out put stream to the client socket  
  19.                      OutputStream out=client.getOutputStream();  
  20.                      //gettimg the date so make the object of callender class  
  21.                      Calendar c=Calendar.getInstance();  
  22.                      System.out.println("server date and time :"+c.getTime());  
  23.                      PrintWriter pw = new PrintWriter(out,true);  
  24.                       pw.close();    
  25.                      }  
  26.               }  
  27.               catch (Exception e)  
  28.               {  
  29.                      System.out.println(e);  
  30.               }                 
  31.        }  
  32. }  
Output
 
First the server is started and waits for a client.
 
cmdserverblank.jpg 
 
Then a Client is started and the output is as follows:
 
cmd of client.jpg 
 
After the client starts then the server output is:
 
cmd after server.jpg 
 
Resources