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. { 
  10. public void static main (String args [ ] )
  11.  { 
  12. try 
  13. { 
  14. // create a server socket ServerSocket s = new ServerSocket(8000);
  15.  
  16. // start listening for connections on srver socket

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






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

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

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