title: flash socket not reading from Java Server properly
body: <p>Since Javascript does not have any functionality by itself to communicate over raw sockets , I decided to use a Java applet - and on the event that the user has disabled java applets on his browser , a flash socket bridge to be integrated with Javascript. I have tried with both <a href="http://code.google.com/p/jsxmlsocket/" rel="nofollow noreferrer">jsxmlsocket</a> as well as <a href="http://haxe.org/doc/js/using_sockets_with_javascript" rel="nofollow noreferrer">haxejssocket</a> only to face the same problem</p>
  
  <p>Now , the server application is written in Java and communication works fine over the java applet. But on flash , there seems to be a problem.</p>
  
  <p>The first text that flash recieves is the response to &lt;policy-file-request/&gt; allowing access to all domains and ports. This works fine , since the sockets start to communicate</p>
  
  <p>Here is the Javascript code that runs when data is received:</p>
  
  <pre><code>socket.onData = function(data) {
      alert(data);
      parse(data);
  }
  </code></pre>
  
  <p>Now , here's the problem. After the response to &lt;policy-file-request/&gt; is sent , 5 more lines of text are sent. What is expected of course is that I will see 5 alert boxes with the text sent in it.
  That doesn't seem to be the case. What I see instead is 5 alert boxed with the response to &lt;policy-file-request/&gt; in it. It looks like something is being sent , cause the OnData function seems to firing off at the correct time , but for some reason , the data seems to be the first line of text sent to it.</p>
  
  <p>Java thread that takes care of sending data:</p>
  
  <pre><code>LinkedBlockingQueue&lt;String&gt; outQueue=new LinkedBlockingQueue&lt;String&gt;(1000);
  public Thread checkSendLoop=new Thread(){ //this is done to cramp everything in one file. Also more neater
      public void run(){
          try{
              OutputStream out=socket.getOutputStream();
              while (true){
                  String mes=outQueue.take();
                  mes=mes.replace("\n", "").replace("\r", ""); //for safety
                  mes=mes+"\0"; //append
                  out.write(mes.getBytes());
                  out.flush();
              }
          }
          catch (Exception e){
              System.out.println("FATAL : Data to a client will not be sent anymore."); //probably a socket close threw the exception
              outQueue.clear();
              outQueue=null;
          }
      }
  };
  </code></pre>
  
  <p>To send data , a send function is called:</p>
  
  <pre><code>public void send(String s){
      Queue&lt;String&gt; testQueue = outQueue;
      s=s+" "+getUnixTime();
      if (testQueue!=null){
          System.out.println("SENDING "+nick+" - "+s);
          testQueue.add(s); //just add to the queue , the check loop will make sure that its sent
      }
  }
  </code></pre>
  
  <p>As you can see , all data sent ends with a null byte or \0. Maybe this is responsible?</p>
  
  <p>NOTE: I want to emphasize here that I am familiar with websockets and how they would be "much more appropriate" , so please do not suggest that.</p>
  
  <p>As mentioned earlier , it works fine in other clients like Telnet and the Java applet.</p>
  
  <p>Update: No, this is not a duplicate of <a href="https://stackoverflow.com/questions/1709467">this question</a>. My problem has to do with the script returning only the response to <code>&lt;policy-file-request/&gt;</code> on every <code>onData</code> call. There is no "error" thrown in the process.</p>