title: Java IO / NIO : Can Server listen on more than one port?
body: <p>I am working on Java IO/NIO. I have implemented many server/client applications for both TCP and UDP (Java IO) and recently started working on NIO. </p>
  
  <p>I want to write a Server(TCP or UDP) which performs more than one task while listening to <strong>more than one port</strong>. I have always implemented server listening to a single port at a time and my question may be against best practices. But is there any way that a server can accept clients on multiple ports? Like more than one server inside a single server?</p>
  
  <p>I am not looking for the code, just the concepts/design of the program if it is possible or not.</p>
  
  <p>Server:</p>
  
  <pre><code>public class ServerAll implements Runnable {
  
  private Socket connection;
  
  ServerAll(Socket s) {
      this.connection = s;
  }
  
  public static void main(String[] args) {
      int port1 = 7777;
      int port2 = 8888;
      int port3 = 9999;
      int count = 0;
      try{
          ServerSocket server1 = new ServerSocket(port1);
          ServerSocket server2 = new ServerSocket(port2);
          ServerSocket server3 = new ServerSocket(port3);
  
          System.out.println("Server Initialized");
          while (true) {
          Socket con1 = server1.accept();
          Runnable runnable1 = new ServerAll(con1);
          Thread thread1 = new Thread(runnable1);
          thread1.start();
  
          Socket con2 = server2.accept();
          Runnable runnable2 = new ServerAll(con2);
          Thread thread2 = new Thread(runnable2);
          thread2.start();
  
          Socket con3 = server3.accept();
          Runnable runnable3 = new ServerAll(con3);
          Thread thread3 = new Thread(runnable3);
          thread3.start();
          }
      }
      catch (Exception e) {}
  }
  
  public void run() {
      try {
          DataInputStream in = new DataInputStream(connection.getInputStream());
          String clientMsg = in.readUTF();
          String replyMsg = null;
  
          if(clientMsg.equalsIgnoreCase("1")){
              String msg = "Server Reply on 1";
              replyMsg=msg;
          }
  
          if(clientMsg.equalsIgnoreCase("2")){
              String msg = "Server Reply on 2";
              replyMsg=msg;
          }
  
          if(clientMsg.equalsIgnoreCase("3")){
              String msg = "Server Reply on 3";
              replyMsg=msg;
          }
  
          DataOutputStream out = new DataOutputStream(connection.getOutputStream());
          out.writeUTF(replyMsg);
      }
      catch (Exception e) {
          System.out.println(e);
      }
      finally {
          try {
              connection.close();
          }
          catch (IOException e){}
      }
  }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>public class ClientTcpE {
  
  public static void main(String[] args) {
      String host = "localhost";
      int port = 7777;
      System.out.println("SocketClient initialized");
      try {
          InetAddress address = InetAddress.getByName(host);
          Socket client = new Socket(address, port);
          System.out.println("Just connected to "+ client.getRemoteSocketAddress());
          OutputStream outToServer = client.getOutputStream();
          DataOutputStream out = new DataOutputStream(outToServer);
          out.writeUTF("1");
          System.out.println("Just sent msg to server");
  
          InputStream inFromServer = client.getInputStream();
          DataInputStream in = new DataInputStream(inFromServer);
          String serverMsg = in.readUTF();
  
          System.out.println("server says : "+serverMsg);
          client.close();
      }catch(IOException e){
          e.printStackTrace();
      }   
  }
  </code></pre>