Write a Java program that implements a simple client server application

Write a Java program that implements a simple client server application

Week  11

Aim: To Write a Java program that implements a simple client/server application . The client sends data to a server. The server receives the data, uses it to produce a result, and then sends the result back to the client. The client displays the result on the console. For ex: The data sent from the client is the radius of a circle, and the result produced by the server is the area of the circle. (Use java.net)

Source Code:

/*Program for the implementation of the client server communication*/

import java.io.*;

import java.net.*;

public class Server

{

           public static void main(String rr[])throws IOException

           {

                  ServerSocket ss=new ServerSocket(8000);

                  Socket s=ss.accept();

                  DataInputStream dis=new DataInputStream(s.getInputStream());

                  PrintStream ps=new PrintStream(s.getOutputStream());

                  String str=dis.readLine();

                  double r=new Double(str).doubleValue();

                  System.out.println(“Radius Given by client:”+r);

                  double area=3.14*r*r;

                  ps.println(area);

                  ss.close();

           }

}

/*Program for the implementation of the client server communication*/

import java.io.*;

import java.net.*;

public class Client

{

           public static void main(String rr[])throws Exception

           {

                  DataInputStream dis1,dis2;

                  String host;

                  PrintStream ps;

                  if(rr.length==0)

                  host=”LocalHost”;

                  else

                  host=rr[0];

                  Socket s=new Socket(host,8000);

                  System.out.println(“Enter the radius:”);

                  dis1=new DataInputStream(System.in);

                  dis2=new DataInputStream(s.getInputStream());

                  double r=new Double(dis1.readLine());

                  ps=new PrintStream(s.getOutputStream());

                  ps.println(r);

                  String a=dis2.readLine();

                  System.out.println(“Area Calculated by server:”+a);

                  s.close();

           }

}

Expected OutPut:

 Write a Java program that implements a simple client/server application

Write a Java program that implements a simple client/server application

https://en.wikipedia.org/wiki/Client%E2%80%93server_model

Leave a Comment

Your email address will not be published. Required fields are marked *