public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; boolean listeningSocket = true; try { serverSocket = new ServerSocket(2343); } catch (IOException e) { System.err.println("Could not listen on port: 2343"); } while (listeningSocket) { Socket clientSocket = serverSocket.accept(); MiniServer mini = new MiniServer(clientSocket); mini.start(); } serverSocket.close(); }
@Override public void run() { while (true) { if (Server.isRunning() == false) { Server.setLock(true); try { serverSocket.close(); if (Server.getClients() > 0) { try { for (MiniServer subClient : util.clients()) { synchronized (subClient) { System.out.println(util.getDate() + subClient.getCId() + " disconnected."); subClient.sendMessage("Server Closed"); subClient.close(); } } util.clients().clear(); } catch (ConcurrentModificationException e) { e.printStackTrace(); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Server.setLock(false); break; } try { Thread.sleep(10); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }