title: Java socket giving premature end of stream
body: <p>I'm setting up a comet server that connects to a XMPP server.  Here's how it goes down:</p>
  
  <p>A client connects with the comet server and, among other things, a socket connection is opened:</p>
  
  <pre><code>try {
          radio = new Socket("server", 5222);
          out = new PrintWriter(radio.getOutputStream(), true);
          in = new BufferedReader(new InputStreamReader(radio.getInputStream()));
      } catch (UnknownHostException e) {
          System.out.println("Unknown host: "+e);
          error = e.toString();
      } catch(IOException e) {
          System.out.println("IO error: "+e);
          error = e.toString();
      }
  </code></pre>
  
  <p>Next, a thread is started, which waits for data from the socket:</p>
  
  <pre><code>public void run() {
          System.out.println("Thread started.");
          String data;
          String error;
          Client remote;
          Client client;
          while(!done) {
              data = this.output();
              remote = bayeux.getClient(remoteId);
              client = bayeux.getClient(clientId);
              if(data!=null) {
                  Map&lt;String, Object&gt; packet = new HashMap&lt;String, Object&gt;();
                  packet.put("xml", data);
                  remote.deliver(client, "/radio/from", packet, null);
              }
              error = this.error();
              if(error!=null) {
                  Map&lt;String, Object&gt; packet = new HashMap&lt;String, Object&gt;();
                  packet.put("details", error);
                  remote.deliver(client, "/radio/error", packet, null);
              }
              /* try { 
                  Thread.sleep(500); 
              }  
              catch (InterruptedException e ) {
                  System.out.println("Interrupted!"); } */
          }
  
          try {
              in.close();
              out.close();
              radio.close();
          } catch(IOException e) {
              System.out.println("Error disconnecting: "+e);
              error = e.toString();
          }
          System.out.println("Thread stopped.");
      }
  
      public String output() {
          try {
              String data = in.readLine();
              System.out.println("From: "+data);
              if(data==null) {
                  System.out.println("End of stream!");
                  try {
                      Thread.currentThread().sleep(1000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  //error = "End of stream.";
                  //this.disconnect();
              }
              return data;
          } catch (IOException e) {
              error = e.toString();
              System.out.println("IO error! "+e);
          }
          return null;
      }
  </code></pre>
  
  <p>Any input received from the client is forwarded to the XMPP server:</p>
  
  <pre><code>    public void input(String xml) {
          System.out.println("To: "+xml);
          out.println(xml);
      }
  </code></pre>
  
  <p>So here's where the problem come in.  The client opens the connection and sends the proper XML to the XMPP server to start a stream.  <code>in.readLine();</code> hangs, as it should, until a response is received from the server.  As soon as it is received, <code>in.readLine();</code> begins to return null, over and over again.  This shouldn't happen; it should hang until it receives data.  It seems unlikely that the server has closed out on me, it hasn't sent the <code>&lt;/stream:stream&gt;</code> to signal the end of an XMPP stream.  Any ideas on what could be the problem?</p>
  
  <p>Thank you for your help!</p>