Write a java program that implements a simple/client application. That 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)

//Program for implementing Server/Client Application
// Server Program
import java.io.*;
import java.net.*;
class Server
{
            public static void main(String args[])
            {
                        try
                        {
                                    ServerSocket ss=new ServerSocket(1064);
                                    System.out.println(“Waiting for Client Request”);
                                    Socket s=ss.accept();
                                    BufferedReader br;
                                    PrintStream ps;
                                    String str;
                                    br=new BufferedReader(new InputStreamReader(s.getInputStream()));
                                    str=br.readLine();
                                    System.out.println(“Received radius”);
                                    double r=Double.parseDouble(str);
                                    double area=3.14*r*r;
                                    ps=new PrintStream(s.getOutputStream());
                                    ps.println(String.valueOf(area));
                                    br.close();
                                    ps.close();
                                    s.close();
                                    ss.close();
                        }
                        catch(Exception e)
                        {
                                    System.out.println(e);
                        }
            }
}
Output:
Waiting for Client Request
Received radius
//Client Program
import java.io.*;
import java.net.*;
class Client
{
            public static void main(String args[])throws IOException
            {
                       
                        Socket s=new Socket(InetAddress.getLocalHost(),1064);
                        BufferedReader br;
                        PrintStream ps;
                        String str;
                        System.out.println(“Enter Radius  :”);
                        br=new BufferedReader(new InputStreamReader(System.in));
                        ps=new PrintStream(s.getOutputStream());
                        ps.println(br.readLine());
                        br=new BufferedReader(new InputStreamReader(s.getInputStream()));
                        str=br.readLine();
                        System.out.println(“Area of the circle is : “+str);
                        br.close();
                        ps.close();
            }
}
Output:
Enter Radius  :
9

Area of the circle is : 254.34

3 Comments

Leave a Reply

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

Enable Notifications OK No thanks