Monday, 13 July 2015

Java Programs[Important]

  1. 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.
  2.  
  3. Program :
  4.  
  5. // Server Program import   .io.*; 
  6. import   .net.*; import   .util.*;
  7.  
  8. public class Server 
  9. public void static main (String args [ ] )
  10.  { 
  11. try 
  12. // create a server socket ServerSocket s = new ServerSocket(8000);
  13.  
  14. // start listening for connections on srver socket

  15. Socket connectToClient = s.accept();
  16.  
  17. // create a buffered reader stream to get data from client BufferedReader isFromClient = new BufferedReader(new InputStreamReader (connectToClient.getInputStream()));
  18.  
  19. // create a buffer reader to send result to client PrintWriter osToClient = new PrintWriter(connectToClient.getOutputStream(), true);
  20.  
  21. // continuously read from client, process, send back while (true) { // read a line and create string tokenizer StringTokenizer st = new StringTokenizer(isFromClient.readLine());
  22.  
  23. //convert string to double double radius = new Double(st.nextToken()).doubleValue();
  24.  
  25. System.out.println(“Radius received from client: “ + radius);
  26.  
  27. // comput area double area = radius * radius *Math.PI;
  28.  
  29. // send result to client osToClient.println(area);
  30.  
  31. // print result on console System.out.println(“Area found: “ +area); }
  32.  }
  33.  catch (IOException e)
  34.  { 
  35. System.err.println(e); 
  36. }
  37.  } 
  38. }






  39. // Client Program import   .io.*;

  40. import   .net.*; import   .util.*;
  41.  
  42. public class Client
  43.  { 
  44. public void static main (String args [ ] )
  45.  {
  46.  try
  47.  { 
  48. // create a socket to connect to server Socket connectToServer = new Socket(“local host”, 8000);
  49.  
  50. // create a buffered input stream to get result from server BufferedReader isFromServer = new BufferedReader(new InputStreamReader (connectToServer.getInputStream()));
  51.  
  52. // create a buffer output stream to send data to server PrintWriter osToServer = new PrintWriter(connectToClient.getOutputStream(), true);
  53.  
  54. // continuously send radius and get area
  55.  while (true)
  56.  { 
  57. Scanner input=new Scanner(System.in);   

  58. System.out.print(“Please enter a radius: “);
  59.  double radius =input.nextDouble();
  60.  
  61. // display radius on console osToServer.println(radius);
  62.  
  63. // get area from server StringTokenizer st = new StringTokenizer(isFromServer.readLine());
  64.  
  65. // convert string to double Double area = new Double(st.nextToken()).doubleValue();
  66.  
  67. // print result on console System.out.println(“Area received from the server is: “ +area); 
  68. }
  69.  }
  70.  catch (IOException e)
  71.  { 
  72. System.err.println(e); 
  73. }