title: How to connect to Java ServerSocket from Remote Host
body: <p>Found some similar questions but no answer, so here goes -</p>
  
  <p>Attached code is self contained, compilable and runnable to set up a server on my computer. Socket gets created, and I can do a <code>telnet &lt;ip_address&gt; 9162</code>  or <code>telnet localhost 9162</code> from my local machine. From a remote host, telnet does not get connected. Any ideas? TIA.</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  
  public class TestServerSocket {
  
          public static void main(String args[]) throws IOException {
                  final int portNumber = 9162;
                  System.out.println("Creating server socket on port " + portNumber);
                  ServerSocket serverSocket = new ServerSocket(portNumber);
                  while (true) {
                          Socket socket = serverSocket.accept();
                          OutputStream os = socket.getOutputStream();
                          PrintWriter pw = new PrintWriter(os, true);
                          BufferedReader br = new BufferedReader(
                                          new InputStreamReader(socket.getInputStream()));
                          while (true) {
                                  pw.print("What's you name? ");
                                  pw.flush();
                                  String str = br.readLine();
                                  if (str.trim().length() == 0)
                                          break;
                                  pw.println("Hello, " + str);
                          }
                          pw.close();
                          socket.close();
                  }
          }
  }
  </code></pre>