Advertisement
Guest User

Untitled

a guest
Sep 13th, 2016
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1.  
  2. public class SocketServer {
  3.  
  4.     public ServerSocket server;
  5.     public static HashMap<Integer, PrintWriter> listOfClients;
  6.     public int id=0;
  7.    
  8.  
  9.     /**
  10.      * Wait for Sockets from clients
  11.      */
  12.     public void listenToClients() {
  13.        
  14.         // Never stop waiting!
  15.         while (true) {
  16.            
  17.             try {
  18.                
  19.                 // Accept socket from this client
  20.                 Socket clientSocket = server.accept();
  21.  
  22.                 // Add PrintWriter (from client) to array
  23.                 PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());
  24.                 listOfClients.put(id, writer);
  25.  
  26.                 // Create and start new thread for this client
  27.                 Thread clientThread = new Thread(new ClientHandler(clientSocket, id));
  28.                 clientThread.start();
  29.                
  30.                 Logger.out("[Client#" + id + "] connected.");
  31.                
  32.                 // Increase ID
  33.                 id++;
  34.             } catch (IOException e) {
  35.                 Logger.out("Can't listen to client(s)! Error code: 002");
  36.                 e.printStackTrace();
  37.             }
  38.         }
  39.     }
  40.  
  41.     /**
  42.      * Runs the SocketServer
  43.      * @return
  44.      */
  45.     public boolean runServer() {
  46.        
  47.         try {
  48.            
  49.             // Start serversocket
  50.             server = new ServerSocket(5555);
  51.             Logger.out("Server started.");
  52.             Logger.out("Waiting for clients to connect...");
  53.  
  54.             // Initialize PrintWriter (from clients) list
  55.             listOfClients = new HashMap<Integer, PrintWriter>();
  56.             return true;
  57.         } catch (IOException e) {
  58.             Logger.out("Server can not be started! Error code: 001");
  59.             e.printStackTrace();
  60.             return false;
  61.         }
  62.     }
  63.  
  64.     /**
  65.      * Sends a message to all connected clients
  66.      * @param message
  67.      */
  68.     public static void sendToAllClients(String message) {
  69.        
  70.         // Go through list and send message to all clients
  71.         for(PrintWriter writer : listOfClients.values()) {
  72.            
  73.             writer.println(message);
  74.             writer.flush();
  75.         }
  76.     }
  77.    
  78.     /**
  79.      * Sends a message to a specific client
  80.      * @param recieverID
  81.      * @param message
  82.      */
  83.     public static void sendToClient(int recieverID, String message) {
  84.  
  85.         //Get client from list and send message
  86.         PrintWriter writer = listOfClients.get(recieverID);
  87.         writer.println(message);
  88.         writer.flush();
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement