Advertisement
gagan93

Server.java MyCampusNotes

Oct 25th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 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.ServerSocket;
  6. import java.net.Socket;
  7.  
  8. public class Server
  9. {
  10.     public static void main(String[] args)
  11.     {
  12.         try
  13.         {
  14.             /* We create a server which listens to port no. 4040 of the computer. */
  15.             ServerSocket ss = new ServerSocket(4040);
  16.  
  17.             /* Server stops at this line unless client connects to it. In a single threaded application like this, we
  18.                          * can handle only one client. The server continues further ONLY WHEN the client connects */
  19.             Socket s = ss.accept();
  20.  
  21.             /* This stream is connected to the console to accept user input */
  22.             BufferedReader readerForConsole = new BufferedReader(
  23.                     new InputStreamReader(System.in));
  24.  
  25.             /*
  26.              * This stream is connected to the socket, to accept data from
  27.              * server
  28.              */
  29.             BufferedReader readerForSocket = new BufferedReader(
  30.                     new InputStreamReader(s.getInputStream()));
  31.  
  32.             /*
  33.              * This stream is connected to the socket, to write data to the
  34.              * server
  35.              */
  36.             PrintWriter writerForSocket = new PrintWriter(s.getOutputStream());
  37.  
  38.             /*
  39.              * now we can use standard readline() to read data from the server
  40.              * and println() to write data to the server
  41.              */
  42.  
  43.             while (true)
  44.             {
  45.                 /* Read data from server */
  46.                 String dataFromServer = readerForSocket.readLine();
  47.  
  48.                 /* write that data to console */
  49.                 System.out.println("< Response > " + dataFromServer);
  50.  
  51.                 /* read some data from console */
  52.                 String dataFromConsole = readerForConsole.readLine();
  53.  
  54.                 /*
  55.                  * write that data to socket so that server can read it. Please
  56.                  * note that using 'flush()' method is important in buffered
  57.                  * streams otherwise data is not sent to the other side unless
  58.                  * buffer is full.
  59.                  */
  60.                 writerForSocket.println(dataFromConsole);
  61.                 writerForSocket.flush();
  62.  
  63.                 /*
  64.                  * This loops infinitely, please use cross on your command
  65.                  * prompt window to exit.
  66.                  *
  67.                  * Ideally we should close the I/O stearms as well as the socket
  68.                  * after completing our work. But that would be unreachable code
  69.                  * here as we are looping infinitely.
  70.                  *
  71.                  * Ideally we should do the following :
  72.                  *
  73.                  *
  74.                  * readerForConsole.close();
  75.                  *
  76.                  * readerForSocket.close();
  77.                  *
  78.                  * writerForSocket.close();
  79.                  *
  80.                  * s.close();
  81.                  */
  82.             }
  83.         }
  84.         catch (IOException e)
  85.         {
  86.             // TODO Auto-generated catch block
  87.             e.printStackTrace();
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement