Advertisement
gagan93

Client.java MyCampusNotes

Oct 25th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.net.Socket;
  6.  
  7. public class Client
  8. {
  9.     public static void main(String[] args)
  10.     {
  11.         try
  12.         {
  13.             /*
  14.              * here 127.0.0.1 is the IP address of computer itself, also known
  15.              * as the address of localhost. If you have kept the server in a
  16.              * different computer but on the same network, put that IP address
  17.              * there. That IP address would be something like (191.168.x.x)
  18.              *
  19.              * 4040 is the port no. on which our server is active (check
  20.              * Server.java file )
  21.              */
  22.             Socket s = new Socket("127.0.0.1", 4040);
  23.  
  24.             /* This stream is connected to the console to accept user input */
  25.             BufferedReader readerForConsole = new BufferedReader(
  26.                     new InputStreamReader(System.in));
  27.  
  28.             /*
  29.              * This stream is connected to the socket, to accept data from
  30.              * server
  31.              */
  32.             BufferedReader readerForSocket = new BufferedReader(
  33.                     new InputStreamReader(s.getInputStream()));
  34.  
  35.             /*
  36.              * This stream is connected to the socket, to write data to the
  37.              * server
  38.              */
  39.             PrintWriter writerForSocket = new PrintWriter(s.getOutputStream());
  40.  
  41.             /*
  42.              * now we can use standard readline() to read data from the server
  43.              * and println() to write data to the server
  44.              */
  45.  
  46.             while (true)
  47.             {
  48.                 /* read some data from console */
  49.                 String dataFromConsole = readerForConsole.readLine();
  50.  
  51.                 /*
  52.                  * write that data to socket so that server can read it. Please
  53.                  * note that using 'flush()' method is important in buffered
  54.                  * streams otherwise data is not sent to the other side unless
  55.                  * buffer is full.
  56.                  */
  57.                 writerForSocket.println(dataFromConsole);
  58.                 writerForSocket.flush();
  59.  
  60.                 /* Read data from server */
  61.                 String dataFromServer = readerForSocket.readLine();
  62.  
  63.                 /* write that data to console */
  64.                 System.out.println("< Response > "+dataFromServer);
  65.  
  66.                 /*
  67.                  * This loops infinitely, please use cross on your command
  68.                  * prompt window to exit.
  69.                  *
  70.                  * Ideally we should close the I/O stearms as well as the socket
  71.                  * after completing our work. But that would be unreachable code
  72.                  * here as we are looping infinitely.
  73.                  *
  74.                  * Ideally we should do the following :
  75.                  *
  76.                  *
  77.                  * readerForConsole.close();
  78.                  *
  79.                  * readerForSocket.close();
  80.                  *
  81.                  * writerForSocket.close();
  82.                  *
  83.                  * s.close();
  84.                  */
  85.             }
  86.  
  87.         }
  88.         catch (IOException e)
  89.         {
  90.             // TODO Auto-generated catch block
  91.             e.printStackTrace();
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement