Java Tutorial

Network Programming

Network Programming

Collection of interconnected autonomous or non-autonomous computers is known as network.

Autonomous represents independent processing power whereas non-autonomous represents dependent processing.

Aims of networking:

  1. Fastest and reliable communication can be achieved.
  2. Communication cost is so less.
  3. Data can be shared either locally (centralized application) or globally (distributed application).

As a part of networking we write two types of programs. They are client side programming and server side programming.

  1. A client side programming is one which always makes a request to get the service from the server side program.
  2. A server side programming is one which receives client request, process the request and gives response back to the client.
aims networking

In order to exchange the data between the client and server we must have a protocol. A protocol is the set of rules which exchange the data between client and server programs.

When we are developing any client and server application we must follow certain steps at client side and server side.

Steps to be performed at client side:

  1. Connect to the server side program. To connect to the server the client side program must pass server name or domain naming services or IP address.
  2. Make a request by passing data.
  3. After performing some operations at server side, the client side program must receive the response from the server side.
  4. Read the response data which is sent by the server.

Steps to be performed at server side:

  1. Every server side program must run at certain port number (a port number is a logical numerical ID at which logical execution of the program is taking place).
  2. Every server must have a physical name to access the services or programs which are running. Server can be access in two ways. On name (localhost by default) called DNS and on IP address (127.0.0.1 default).
  3. Every server side program must receive client request.
  4. Every server side program must read request data (request parameter).
  5. Process the data or request.
  6. Send the response back to the client.

Socket: In order to develop the program at client side we must use the class Socket.

Socket API:

Constructor:

Socket (String hname, int Portno) throws UnknownHostException, IOException

Instance methods:

public OutputStream getOutputStream (); - 1 
public InputStream getInputStream (); - 2 
public void close (); - 3

Method-1 is used for establishing the connection with server socket by passing host name and port number of the server side program. Method-2 is used for writing the data to the server side program. Method-3 is used for receiving or reading the response given by the server side program. Method-4 is used for closing socket or client communication with the server.

Socket s=new Socket ("localhost", 7001);

ServerSocket: In order to develop the program at server side we must use the class ServerSocket.

ServerSocket API:

Constructor:

ServerSocket (int portno) throws IOException - 1

Instance methods:

public Socket accept (); - 2 
public void close (); - 3

Method-1 is used for making the server side program to run at certain port number. Method-2 is used for accepting socket data (client data). Method-3 is used for closing or terminating server side program i.e., ServerSocket program.

Write a java program which illustrates the concept of Socket and ServerSocket classes?

Answer:

server.java:

import java.net.*;
import java.io.*;

class server {

    public static void main(String[] args) {
        try {
            int pno = Integer.parseInt(args[0]);
            ServerSocket ss = new ServerSocket(pno);
            System.out.println("SERVER IS READY");
            while (true) {
                Socket s = ss.accept();
                InputStream is = s.getInputStream();
                DataInputStream dis = new DataInputStream(is);
                int n = dis.readInt();
                System.out.println("VALUE OF CLIENT = " + n);
                int res = n * n;
                OutputStream os = s.getOutputStream();
                DataOutputStream dos = new DataOutputStream(os);
                dos.writeInt(res);
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }
};

client.java:

import java.io.*;
import java.net.*;

class client {

    public static void main(String[] args) {
        try {
            String sname = args[0];
            int pno = Integer.parseInt(args[1]);
            Socket s = new Socket(sname, pno);
            System.out.println("CLIENT CONNECTED TO SERVER");
            OutputStream os = s.getOutputStream();
            DataOutputStream dos = new DataOutputStream(os);
            int n = Integer.parseInt(args[2]);
            dos.writeInt(n);
            InputStream is = s.getInputStream();
            DataInputStream dis = new DataInputStream(is);
            int res = dis.readInt();
            System.out.println("RESULT FROM SERVER = " + res);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
};

Disadvantages of networking:

  1. We are able to develop only one-one communication or half-duplex or walky talky applications only.
  2. We are able to get only language dependency (client side and server side we must write only java programs).
  3. There is no support of predefined protocol called http.
  4. There is no internal support of third party servers such as tomcat, weblogic, etc.
  5. We are able to develop only intranet applications but not internet applications.