title: Java - connection to ServerSocket via browser/URL
body: <p>I'm writing a piece of software, and I'm under the restriction of not being able to use socket to connect to a java application using a ServerSocket.</p>
  
  <p>I thought I'd try with an URL connection, since it's possible to define which port to connect to</p>
  
  <p>e.g:</p>
  
  <pre><code>127.0.0.1:62666
  </code></pre>
  
  <p>I have my server app listening for connections and writing the input out to a jTextArea. When connecting to the server (127.0.0.1:62666) through a browser, it outputs:</p>
  
  <pre><code>GET / HTTP/1.1
  GET /favicon.ico HTTP/1.1
  </code></pre>
  
  <p>I have another app for connecting to the ServerSocket through an URL connection:</p>
  
  <pre><code>try{
          URL url = new URL("http://127.0.0.1:62666");
          URLConnection connection = url.openConnection();
          connection.setDoOutput(true);
          connection.connect();
          PrintWriter writer = new PrintWriter(connection.getOutputStream());
          writer.print("Hello");
          System.out.println("should have worked");
          writer.flush();
          writer.close();
      }catch(IOException e){
          e.printStackTrace();
      }
  </code></pre>
  
  <p>It prints out the "should have worked" message fyi, but it never writes anything to the jTextArea of the server. The code for the server app looks like this:</p>
  
  <pre><code>try{
  
          ServerSocket serverSock = new ServerSocket(62666);
  
          while(doRun){
              Socket sock = serverSock.accept();
              BufferedReader reader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
              PrintWriter writer = new PrintWriter(sock.getOutputStream());
  
              InfoReader.gui.writeToTextArea(reader.readLine() + " From IP: " + sock.getInetAddress() + "\n");
              writer.println("Testing123");
  
              writer.close();
  
              reader.close();
  
          }
      }catch(IOException e){
          e.printStackTrace();
      }
  </code></pre>
  
  <p>Note: when connecting through the browser it displays the text "Testing123".</p>
  
  <p>So I'm wondering how to do this the way I'm trying or perhaps read the URL that the ServerSocket was accessed through, so I could access it through a URL while passing it arguments (in the URL).</p>
  
  <p>Hope this makes sense :)</p>
  
  <p>Thanks,
  Mike.</p>