Hi,all .How would i make client/server app,to calculate surface area of the cylider.
The server must read the cylinders radius and length from the client and calculate the surface area of a cylinder. The server will then return a double representing the surface area of a cylinder to the client. The client must then display the surface area (formatted to two decimal places) to the user
this is my client :
package clientserverapp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import javax.swing.JOptionPane;
public class Client {
public static void main(String[] args) throws Exception{
Client clientSocket = new Client();
clientSocket.run();
}
public void run()throws Exception {
Socket SOCK = new Socket("localhost", 444);
PrintStream PS = new PrintStream(SOCK.getOutputStream());
double p =3.14;
String radiusInput = JOptionPane.showInputDialog("Enter Radius");
double r = Double.parseDouble(radiusInput);
String heightInput = JOptionPane.showInputDialog("Enter Height");
double h = Double.parseDouble(heightInput);
double surface = (2.0 * r * p * h) + (2.0 * p * r * r);
JOptionPane.showMessageDialog(null, surface);
PS.println("Calculating...");
InputStreamReader IR = new InputStreamReader(SOCK.getInputStream());
BufferedReader BR = new BufferedReader(IR);
String message = BR.readLine();
System.out.println(message);
}
}
And here server :
package clientserverapp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) throws Exception{
Server aServer = new Server();
aServer.run();
}
public void run() throws Exception{
ServerSocket servSocket = new ServerSocket(444);
Socket socket = servSocket.accept();
InputStreamReader IR = new InputStreamReader(socket.getInputStream());
BufferedReader BR = new BufferedReader(IR);
String message = BR.readLine();
System.out.println(message);
if(message!=null)
{
PrintStream PS = new PrintStream(socket.getOutputStream());
Client c = new Client();
c.run();
PS.println("Message received");
}
}
}