title: Communication between Flex and Java Socket
body: <p>I have a Flex AIR app, and i am trying to get data from a Java Server. Can some one tell me if this approach will work?</p>
  
  <p>Start a ServerSocket in Java and wait for a connection.<br>
  Use Flex to connect to the same port.<br>
  Write some data to the socket from Java Server.<br>
  Read the data at the Flex end and process it.</p>
  
  <p>--
  Update :
  I think i am able to get this working, but the data that i read back at Flex end seems to be empty.</p>
  
  <p>My server code is :</p>
  
  <pre><code>        socket  = new ServerSocket(port);
          client  = socket.accept();
  
          InetAddress address = client.getInetAddress();           
  
          BufferedReader in = new BufferedReader( new InputStreamReader(
                  client.getInputStream()));
          PrintWriter out = new PrintWriter(client.getOutputStream());            
  
          out.println("hi");
          out.flush();
  </code></pre>
  
  <p>And my code in Flex is </p>
  
  <pre><code>        private function onRecieveDataClick():void
          {
              var host:String = "127.0.0.1";
              var port:int = 9090;
  
              var socket:Socket = new Socket();
              socket.endian = Endian.BIG_ENDIAN;
              socket.addEventListener(Event.CONNECT, onConnect);
              socket.connect(host, port);
          }
  
          private function onConnect(event:Event):void
          {
              trace(" Connected to server socket ");
              var socket:Socket = Socket(event.target);
              var obj:String = socket.readUTFBytes( socket.bytesAvailable);
              trace(obj);
          }
  </code></pre>
  
  <p>My current problem is <code>socket.bytesAvailable</code> becoming 0 and therefore <code>obj</code> is coming up as "". So how to read data sent from the server using a <code>Socket</code> in Flex?</p>