title: Java - ServerSocket accept() method repeating in while(true) loop
body: <p>Hopefully someone can shed some light on this one, and provide a workaround, or illuminate some method I'm missing here.</p>
  
  <p>Just messing around trying to create a really simple web server.</p>
  
  <p>So far I have:</p>
  
  <pre><code>public class SocketTest {
  
      public static void main(String[] args) throws IOException {
  
          ServerSocket sock = new ServerSocket(9876);
  
          while(true) {
  
              System.out.println("Before");
              Socket conn = sock.accept();
              System.out.println("After");
  
              conn.close();
  
          }
      }
  }
  </code></pre>
  
  <p>Then, once the project is running, the output window displays:</p>
  
  <pre><code>Before
  </code></pre>
  
  <p>Which is what I expect, but then as soon as I type: </p>
  
  <pre><code>127.0.0.1:9876
  </code></pre>
  
  <p>Into a web browser, I end up with:</p>
  
  <pre><code>Before
  After
  Before
  After
  Before
  After
  Before
  </code></pre>
  
  <p>Showing. So the browser is obviously connecting to that port multiple times, but only the first connection contains any info in its page header.</p>
  
  <hr>
  
  <p>While writing this, I've decided to go a little further with my experimentation's, and come up with:</p>
  
  <pre><code>public class SocketTest {
  
      public static void main(String[] args) throws IOException {
  
          ServerSocket sock = new ServerSocket(9876);
  
          while(true) {
  
              System.out.println("Before");
              Socket conn = sock.accept();
              System.out.println("After");
  
              Scanner sc = new Scanner(conn.getInputStream());
  
              System.out.println("BEFORE if...");
  
              if (sc.hasNext()) {
  
                  System.out.println("INSIDE if...");
  
                  String[] cnr = sc.nextLine().split("\\s+");
                  System.out.println("Command: " + cnr[0] + "  -  URL: " + cnr[1]);
  
              } else {
  
                  System.out.println("INSIDE ELSE!");
  
              }
  
              System.out.println("Closing...");
  
              conn.close();
  
              System.out.println("Closed!");
          }
      }
  }
  </code></pre>
  
  <p>If you compile and run this code, you'll notice that upon first opening a page on the socket, you get the repeated socket opening, but it also appears to hang for about 10 seconds at the line:</p>
  
  <pre><code>if (sc.hasNext()) {
  </code></pre>
  
  <p>As though it's watching the InputStream() for a set period of time, to see if it contains any information or not..?</p>
  
  <p>Also, if I send two actual pages in quick succession (or just refresh the page), it goes through immediately.</p>
  
  <p>Can anyone shed some light on this?</p>
  
  <p>Either how I can remove the waiting on an InputStream() that's never going to materialise, or how I can get it to ignore the <em>phantom</em> sockets.</p>
  
  <p>Even a bit of background as to what's causing the anomaly would be great!</p>