title: Data transfer using sockets. Java & Firefox extension
body: <p>I am trying to implement a data transfer between programs in Java (Client) and Firefox extension in JavaScript (Server). I can't send and receive using one socket only. Help me to solve this problem, please. Right now the client gets a message from the server, but gives it a blank message.</p>
  
  <p>Client (Java):</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.io.PrintWriter;
  import java.net.Socket;
  import java.net.SocketOptions;
  
  public class JustForTest {
  
      /**
       * @param args the command line 
       * 
       * Connect to FF Plugin (Server). Send URL and waiting for innerHTML
       */
      public static void main(String[] args) throws IOException {
          String messageFromServer;
          String messageToServer = "http://google.pl/";
  
          Socket clientSocket = new Socket("localhost", 8888);
  
          try {
              System.out.println("Socket is connected to 127.0.0.1:8888");
              // Send URL to Firefox Plugin
              PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
              out.write(messageToServer);
              InputStream inStream = clientSocket.getInputStream();
              // Is waiting for answer from Firefox Plugin
              BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
              System.out.println(reader.readLine());
  
          } catch (IOException e) {
              e.printStackTrace();
          } finally {
              if (clientSocket != null &amp;&amp; clientSocket.isConnected()) {
                  try {
                      clientSocket.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                      System.out.print(e);
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>Firefox extension (JavaScript):</p>
  
  <pre><code>    var BeansServer = new function () {
  
      /*
       * Listen for connections. Transmit
       */
      var listener = {
          onSocketAccepted: function(serverSocket, transport) {
              addSpan("Accepted connection on " + transport.host + ":" + transport.port);
              var input = transport.openInputStream(Ci.nsITransport.OPEN_BLOCKING, 0, 0);//.QueryInterface(Ci.nsIAsyncInputStream);
              var output = transport.openOutputStream(Ci.nsITransport.OPEN_BLOCKING, 0, 0);
              var sin = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(Ci.nsIScriptableInputStream);
  
              try{
                  sin.init(input);
                  var readBytes = sin.available();
  
                  var request = '';
                  request = sin.read(readBytes);
                  addSpan('Received: ' + request);
  
                  getUrl(request);
  
                  output.write("yes", "yes".length);
                  output.flush();
              }
              finally{
                  sin.close();
                  input.close();
                  output.close();
              }
          }
      }
  
      /*
       * Main
       */
      this.start = function() {
          var serverSocket = Cc["@mozilla.org/network/server-socket;1"].createInstance(Ci.nsIServerSocket);
          serverSocket.init(8888, true, 5);
          addSpan("Opened socket on " + serverSocket.port);
          serverSocket.asyncListen(listener);
      };
  
      /*
       * Opening the page in the browser. Checks the HTML status of the loaded page
       */
      function getUrl(url){
          var xmlhttp = getXmlHttp();
          xmlhttp.open('GET', 'http://google.pl', true);
          xmlhttp.onreadystatechange = function() {
              if (xmlhttp.readyState == 4) {
                  if(xmlhttp.status == 200) {
                      alert(xmlhttp.status);
                  }
              }
          };
          xmlhttp.send(null);
      }
  
      function getXmlHttp(){
          var xmlhttp;
          try {
              xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
          } catch (e) {
              try {
                  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
              } catch (E) {
                  xmlhttp = false;
              }
          }
          if (!xmlhttp &amp;&amp; typeof XMLHttpRequest!='undefined') {
              xmlhttp = new XMLHttpRequest();
          }
          return xmlhttp;
      }
  
      function getBrowser(){
          if (!gBrowser)
              gBrowser = document.getElementById("content");
          return gBrowser;
      }
  
      /*
       * Displays a string on a page in the browser
       */
      function addSpan(content2) {
          var mySpan = content.document.createElement('span');
          mySpan.innerHTML = content2 + '&lt;br/&gt;';
          content.document.body.appendChild(mySpan);
      }
  }
  </code></pre>