TermSpar

Java Multiclient CLIENT

Apr 23rd, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. package myNetwork;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.PrintStream;
  5. import java.io.BufferedReader;
  6. import java.io.InputStreamReader;
  7. import java.io.IOException;
  8. import java.net.Socket;
  9. import java.net.UnknownHostException;
  10.  
  11. public class myClient implements Runnable {
  12.  
  13.   // The client socket
  14.   private static Socket clientSocket = null;
  15.   // The output stream
  16.   private static PrintStream os = null;
  17.   // The input stream
  18.   private static DataInputStream is = null;
  19.  
  20.   private static BufferedReader inputLine = null;
  21.   private static boolean closed = false;
  22.  
  23.   public static void main(String[] args) {
  24.  
  25.     // The default port.
  26.     int portNumber = 2222;
  27.     // The default host.
  28.     String host = "localhost";
  29.  
  30.     if (args.length < 2) {
  31.       System.out
  32.           .println("Usage: java MultiThreadChatClient <host> <portNumber>\n"
  33.               + "Now using host=" + host + ", portNumber=" + portNumber);
  34.     } else {
  35.       host = args[0];
  36.       portNumber = Integer.valueOf(args[1]).intValue();
  37.     }
  38.  
  39.     /*
  40.      * Open a socket on a given host and port. Open input and output streams.
  41.      */
  42.     try {
  43.       clientSocket = new Socket(host, portNumber);
  44.       inputLine = new BufferedReader(new InputStreamReader(System.in));
  45.       os = new PrintStream(clientSocket.getOutputStream());
  46.       is = new DataInputStream(clientSocket.getInputStream());
  47.     } catch (UnknownHostException e) {
  48.       System.err.println("Don't know about host " + host);
  49.     } catch (IOException e) {
  50.       System.err.println("Couldn't get I/O for the connection to the host "
  51.           + host);
  52.     }
  53.  
  54.     /*
  55.      * If everything has been initialized then we want to write some data to the
  56.      * socket we have opened a connection to on the port portNumber.
  57.      */
  58.     if (clientSocket != null && os != null && is != null) {
  59.       try {
  60.  
  61.         /* Create a thread to read from the server. */
  62.         new Thread(new myClient()).start();
  63.         while (!closed) {
  64.           os.println(inputLine.readLine().trim());
  65.         }
  66.         /*
  67.          * Close the output stream, close the input stream, close the socket.
  68.          */
  69.         os.close();
  70.         is.close();
  71.         clientSocket.close();
  72.       } catch (IOException e) {
  73.         System.err.println("IOException:  " + e);
  74.       }
  75.     }
  76.   }
  77.  
  78.   /*
  79.    * Create a thread to read from the server. (non-Javadoc)
  80.    *
  81.    * @see java.lang.Runnable#run()
  82.    */
  83.   @SuppressWarnings("deprecation")
  84. public void run() {
  85.     /*
  86.      * Keep on reading from the socket till we receive "Bye" from the
  87.      * server. Once we received that then we want to break.
  88.      */
  89.     String responseLine;
  90.     try {
  91.       while ((responseLine = is.readLine()) != null) {
  92.         System.out.println(responseLine);
  93.         if (responseLine.indexOf("*** Bye") != -1)
  94.           break;
  95.       }
  96.       closed = true;
  97.     } catch (IOException e) {
  98.       System.err.println("IOException:  " + e);
  99.     }
  100.   }
  101. }
Add Comment
Please, Sign In to add comment