Advertisement
TermSpar

Java Multiclient SERVER

Apr 23rd, 2016
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.69 KB | None | 0 0
  1. package myNetwork;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.PrintStream;
  5. import java.io.IOException;
  6. import java.net.Socket;
  7. import java.net.ServerSocket;
  8.  
  9. /*
  10.  * A chat server that delivers public and private messages.
  11.  */
  12. public class myServer {
  13.  
  14.   // The server socket.
  15.   private static ServerSocket serverSocket = null;
  16.   // The client socket.
  17.   private static Socket clientSocket = null;
  18.  
  19.   // This chat server can accept up to maxClientsCount clients' connections.
  20.   private static final int maxClientsCount = 10;
  21.   private static final clientThread[] threads = new clientThread[maxClientsCount];
  22.  
  23.   public static void main(String args[]) {
  24.  
  25.     // The default port number.
  26.     int portNumber = 2222;
  27.     if (args.length < 1) {
  28.       System.out
  29.           .println("Usage: java MultiThreadChatServer <portNumber>\n"
  30.               + "Now using port number=" + portNumber);
  31.     } else {
  32.       portNumber = Integer.valueOf(args[0]).intValue();
  33.     }
  34.  
  35.     /*
  36.      * Open a server socket on the portNumber (default 2222). Note that we can
  37.      * not choose a port less than 1023 if we are not privileged users (root).
  38.      */
  39.     try {
  40.       serverSocket = new ServerSocket(portNumber);
  41.     } catch (IOException e) {
  42.       System.out.println(e);
  43.     }
  44.  
  45.     /*
  46.      * Create a client socket for each connection and pass it to a new client
  47.      * thread.
  48.      */
  49.     while (true) {
  50.       try {
  51.         clientSocket = serverSocket.accept();
  52.         int i = 0;
  53.         for (i = 0; i < maxClientsCount; i++) {
  54.           if (threads[i] == null) {
  55.             (threads[i] = new clientThread(clientSocket, threads)).start();
  56.             break;
  57.           }
  58.         }
  59.         if (i == maxClientsCount) {
  60.           PrintStream os = new PrintStream(clientSocket.getOutputStream());
  61.           os.println("Server too busy. Try later.");
  62.           os.close();
  63.           clientSocket.close();
  64.         }
  65.       } catch (IOException e) {
  66.         System.out.println(e);
  67.       }
  68.     }
  69.   }
  70. }
  71.  
  72. /*
  73.  * The chat client thread. This client thread opens the input and the output
  74.  * streams for a particular client, ask the client's name, informs all the
  75.  * clients connected to the server about the fact that a new client has joined
  76.  * the chat room, and as long as it receive data, echos that data back to all
  77.  * other clients. When a client leaves the chat room this thread informs also
  78.  * all the clients about that and terminates.
  79.  */
  80. class clientThread extends Thread {
  81.  
  82.   private DataInputStream is = null;
  83.   private PrintStream os = null;
  84.   private Socket clientSocket = null;
  85.   private final clientThread[] threads;
  86.   private int maxClientsCount;
  87.  
  88.   public clientThread(Socket clientSocket, clientThread[] threads) {
  89.     this.clientSocket = clientSocket;
  90.     this.threads = threads;
  91.     maxClientsCount = threads.length;
  92.   }
  93.  
  94.   @SuppressWarnings("deprecation")
  95. public void run() {
  96.     int maxClientsCount = this.maxClientsCount;
  97.     clientThread[] threads = this.threads;
  98.  
  99.     try {
  100.       /*
  101.        * Create input and output streams for this client.
  102.        */
  103.       is = new DataInputStream(clientSocket.getInputStream());
  104.       os = new PrintStream(clientSocket.getOutputStream());
  105.       os.println("Enter your name.");
  106.       String name = is.readLine().trim();
  107.       os.println("Hello " + name
  108.           + " to our chat room.\nTo leave enter /quit in a new line");
  109.       for (int i = 0; i < maxClientsCount; i++) {
  110.         if (threads[i] != null && threads[i] != this) {
  111.           threads[i].os.println("*** A new user " + name
  112.               + " entered the chat room !!! ***");
  113.         }
  114.       }
  115.       while (true) {
  116.        String line = is.readLine();
  117.         if (line.startsWith("/quit")) {
  118.           break;
  119.         }
  120.         for (int i = 0; i < maxClientsCount; i++) {
  121.           if (threads[i] != null) {
  122.             threads[i].os.println("<" + name + "&gr; " + line);
  123.           }
  124.         }
  125.       }
  126.       for (int i = 0; i < maxClientsCount; i++) {
  127.         if (threads[i] != null && threads[i] != this) {
  128.           threads[i].os.println("*** The user " + name
  129.               + " is leaving the chat room !!! ***");
  130.         }
  131.       }
  132.       os.println("*** Bye " + name + " ***");
  133.  
  134.       /*
  135.        * Clean up. Set the current thread variable to null so that a new client
  136.        * could be accepted by the server.
  137.        */
  138.       for (int i = 0; i < maxClientsCount; i++) {
  139.         if (threads[i] == this) {
  140.           threads[i] = null;
  141.         }
  142.       }
  143.  
  144.       /*
  145.        * Close the output stream, close the input stream, close the socket.
  146.        */
  147.       is.close();
  148.       os.close();
  149.       clientSocket.close();
  150.     } catch (IOException e) {
  151.     }
  152.   }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement