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:
As a part of networking we write two types of programs. They are client side programming and server side programming.
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:
Steps to be performed at server side:
Socket: In order to develop the program at client side we must use the class Socket.
Socket (String hname, int Portno) throws UnknownHostException, IOException
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 (int portno) throws IOException - 1
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:
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); } } };
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); } } };