Exemplo n.º 1
0
  private void runServer() {
    String logFile = properties.getProperty("logFile");
    Utils.setLogFile(logFile, Server.class.getName());
    int port = Integer.parseInt(properties.getProperty("port"));
    String ip = properties.getProperty("serverIp");

    Logger.getLogger(Server.class.getName())
        .log(Level.INFO, "Server started. Listening on: " + port + ", bound to: " + ip);
    try {
      serverSocket = new ServerSocket();
      serverSocket.bind(new InetSocketAddress(ip, port));
      do {
        Socket socket = serverSocket.accept(); // Important Blocking call
        Logger.getLogger(Server.class.getName()).log(Level.INFO, "Connected to a client");

        ClientHandler tmpClient = new ClientHandler(socket, this);
        clients.add(tmpClient);
        tmpClient.start();

      } while (keepRunning);
    } catch (IOException ex) {
      Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
      Utils.closeLogger(Server.class.getName());
    }
  }
Exemplo n.º 2
0
 public static void main(String[] args) {
   try {
     int port = Integer.parseInt(properties.getProperty("port"));
     String ip = properties.getProperty("serverIp");
     String logFile = properties.getProperty("logFile");
     Logger.getLogger(EchoServer.class.getName()).log(Level.INFO, "Server started");
     Utils.setLogFile(logFile, EchoServer.class.getName());
     ServerSocket serverSocket = new ServerSocket();
     serverSocket.bind(new InetSocketAddress(ip, port));
     EchoServer server = new EchoServer();
     server.listen(serverSocket);
   } catch (IOException ex) {
     Logger.getLogger(EchoServer.class.getName()).log(Level.SEVERE, "Connection error: ", ex);
   }
 }