Advertisement
Guest User

ChatServer

a guest
Apr 24th, 2014
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.92 KB | None | 0 0
  1. /**
  2.  *
  3.  */
  4. package chatServer;
  5.  
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.io.PrintWriter;
  10. import java.net.ServerSocket;
  11. import java.net.Socket;
  12. //import java.util.Queue;
  13. /**
  14.  * @author Patch
  15.  *
  16.  */
  17. public class ChatServer extends Thread{
  18.     private int id = -1;
  19.     private static int port = 5689;
  20.     private static boolean debug;
  21.     private Socket clientSocket = null;
  22.     private static ServerSocket serverSocket = null;
  23.     private static final int MAX_CONNECTIONS = 10000;
  24.     private static ChatServer[] serverThread = new ChatServer[MAX_CONNECTIONS];
  25.     //TODO: private [] freeSlots;
  26.     //private static Queue serverThreads = null; - An other possibility..
  27.     private static int clientCount = 0;
  28.     private BufferedReader in = null;
  29.     private PrintWriter out = null;
  30.    
  31.     public ChatServer(Socket client, boolean debug, int port, int id) {
  32.         this.clientSocket = client;
  33.         this.debug = debug;
  34.         this.port = port;
  35.         this.id = id;
  36.     }
  37.    
  38.     public void run() {
  39.         System.out.println("Starting Server Thread!");
  40.  
  41.         try {
  42.             this.in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  43.             this.out = new PrintWriter(clientSocket.getOutputStream(),true);
  44.  
  45.             String inputLine;
  46.             while ((inputLine = in.readLine()) != null) {
  47.                 if (debug) {
  48.                     System.out.println("Debug: " + inputLine);         
  49.                 }
  50.                 synchronized (serverThread) {
  51.                     for(int i=0; i < clientCount; i++) {
  52.                         if(serverThread[i] != null && serverThread[i].out != null && serverThread[i]!= this) {
  53.                             serverThread[i].out.println(inputLine);
  54.                            
  55.                         }
  56.                     }
  57.                 }
  58.                
  59.             }
  60.             synchronized(this) {
  61.                 in.close();
  62.                 out.close();
  63.                 clientSocket.close();
  64.                 serverThread[this.id] = null;
  65.                 System.out.println("Client Disconnected");
  66.             }
  67.            
  68.         }
  69.         catch (IOException e) {
  70.             System.err.print("Unbekannter Fehler: Kein Fehler!");
  71.             e.printStackTrace();
  72.         }
  73.  
  74.     }
  75.     private static void parseArgs(String[] args) {
  76.         switch (args.length) {
  77.             case 0: debug = false; port = 5678; break;
  78.            
  79.             case 1: if (args[0].equalsIgnoreCase("-debug"))
  80.                         {debug = true; port = 5678;}
  81.                     if (args[0].matches("[-+]?\\d*\\.?\\d+") && 1023 <= Integer.parseInt(args[0]) && Integer.parseInt(args[0]) <= 49151)
  82.                         {port = Integer.parseInt(args[0]);} break;
  83.                        
  84.             case 2: if(args[0].equalsIgnoreCase("-debug"))
  85.                         {debug = true;}
  86.                     if(1023 <= Integer.parseInt(args[1]) && Integer.parseInt(args[1]) <= 49151)
  87.                         {port = Integer.parseInt(args[1]);}break;
  88.                        
  89.             default: System.err.println("Usage: java EchoServer [-debug][<port>]"); System.exit(1); break;
  90.         }
  91.     }
  92.     private static void send(Socket client,String msg) throws IOException {
  93.         PrintWriter out = new PrintWriter(client.getOutputStream());
  94.         out.println(msg);
  95.         out.close();
  96.     }
  97.     /**
  98.      * @param args
  99.      */
  100.     public static void main(String[] args) {
  101.         parseArgs(args);
  102.         try {serverSocket = new ServerSocket(port);
  103.         System.out.println("Server running on port " + port +". Debug: " + debug);
  104.             while (true) {
  105.                 System.out.println("Wait for client connection to accept. (Connected Clients: " + clientCount + ")");
  106.                 Socket client = null;
  107.  
  108.                 //synchronized (serverThread[clientCount]) {
  109.                        
  110.                     if(clientCount < MAX_CONNECTIONS) {
  111.                                    
  112.                         client = serverSocket.accept(); //TODO                     
  113.                         System.out.println("Client accepted");
  114.                         synchronized(serverThread) {
  115.                             (serverThread[clientCount] = new ChatServer(client, debug, port, clientCount)).start();
  116.                             clientCount++;
  117.                         }
  118.                        
  119.                     }
  120.                     else  {
  121.                         System.err.println("Maximum Connection reached!");
  122.                         send(client,"Server buys!");
  123.                         }
  124.                  }
  125.             //}
  126.         }
  127.         catch (IOException e) {
  128.             System.out.println("Exception caught when trying to listen on port "
  129.                 + port + " or listening for a connection");
  130.             System.err.println(e.getMessage());
  131.         }
  132.  
  133.     }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement