title: Java Sockets with Openshift
body: <p>Through the use of many articles and Stack Overflow questions, I was finally able to successfully bind a Java Socket to a port (port 8000) on an Openshift website. </p>
  
  <p>I know the binding was successful because no exceptions were thrown, and I was able to poll the Socket for its port number. I can also successfully connect to the socket from a client application I have, after using the rhc client tools to forward the ports.</p>
  
  <p>The problem is, how do I connect to the server socket without port forwarding? I'm not able to connect to the server from any other computers, or from my own computer without port forwarding.</p>
  
  <p>These are the things that I have tried so far:</p>
  
  <ul>
  <li>Connecting to "localhost" at port 8000, which works after port forwarding.</li>
  <li>Connecting to the website URL at port 8000, which always returns null.</li>
  <li>Connecting to the website IP at port 8000, which results in a "Connection refused: connect" error.</li>
  </ul>
  
  <p>Client code: </p>
  
  <pre><code>    InetAddress ip = InetAddress.getByName("localhost"); 
          //Also tried '127.5.8.129' which openshift says is the website IP, and the website URL itself.
      Socket socket = new Socket(ip, 8000);
  
      PrintWriter out = new PrintWriter(socket.getOutputStream());
      BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  
      out.println("Hello World");
      out.flush();
  
      System.out.println(in.readLine());
  
      socket.close();
  </code></pre>
  
  <p>EDIT: I can also bind the Server Socket to port 8443, but still can't access it remotely.</p>