title: Java Socket Server
body: <p>I am already loosing it...</p>
  
  <p>I've built a simple Flash application via FlashDevelop (AS3) and I want it to communicate to a Server. I created then a simple Socket Java Application with the code:</p>
  
  <p>Main.java:</p>
  
  <pre><code>import org.xsocket.connection.*;
  
  public class Main
  {
      protected static IServer srv = null;
  
      public static void main(String[] args) 
      {
          try
          {
              srv = new Server(8090, new xSocketDataHandler()); 
              srv.run();
          }
          catch(Exception ex)
          {
              System.out.println(ex.getMessage());
          }
      }
  
      protected static void shutdownServer()
      {
          try
          {
              srv.close();
          }
          catch(Exception ex)
          {
              System.out.println(ex.getMessage());
          }        
      }
  }
  </code></pre>
  
  <p>and xSocketDataHandler.java:</p>
  
  <pre><code>import java.io.IOException;
  import java.nio.BufferUnderflowException;
  import java.nio.channels.ClosedChannelException;
  import java.util.*;
  import org.xsocket.*;
  import org.xsocket.connection.*;
  
  
  public class xSocketDataHandler implements IDataHandler, IConnectHandler, IDisconnectHandler
  {
      private Set&lt;INonBlockingConnection&gt; sessions = Collections.synchronizedSet(new HashSet&lt;INonBlockingConnection&gt;());
  
      public boolean onData(INonBlockingConnection nbc) throws IOException, BufferUnderflowException, ClosedChannelException, MaxReadSizeExceededException 
      {
          try
          {
              String data = nbc.readStringByDelimiter("\0");
  
              if(data.trim().length() &gt; 0)
              {   
                  System.out.println("Incoming data: " + data);
  
                  if(data.equalsIgnoreCase("&lt;policy-file-request/&gt;"))
                  {
                      nbc.write("&lt;cross-domain-policy&gt;" +
                              "&lt;allow-access-from domain=\"*\" secure=\"false\" /&gt;" +
                              "&lt;allow-access-from domain=\"*\" to-ports=\"8090\"/&gt;" +
                              "&lt;/cross-domain-policy&gt;\0");
                      return true;
                  }
  
                  String[] message = data.split("~");
  
                  sendMessageToAll(nbc, message[0], message[1]);
  
                  if(message[1].equalsIgnoreCase("SHUTDOWN"))
                      Main.shutdownServer();                
              }
          }
          catch(Exception ex)
          {
              System.out.println("onData: " + ex.getMessage());
          }
  
          return true;
      }
  
      private void sendMessageToAll(INonBlockingConnection nbc, String user, String message)
      {
          try
          {
              synchronized(sessions)
              {
                  Iterator&lt;INonBlockingConnection&gt; iter = sessions.iterator();
  
                  while(iter.hasNext())
                  {
                      INonBlockingConnection nbConn = (INonBlockingConnection) iter.next();
  
                      if(nbConn.isOpen())
                          nbConn.write("&lt;b&gt;" + user + "&lt;/b&gt;: " + message + "&lt;br /&gt;\0");
                  }
              }
  
              System.out.println("Outgoing data: " + user + ": " + message);
          }
          catch(Exception ex)
          {
              System.out.println("sendMessageToAll: " + ex.getMessage());
          }            
      }
  
      public boolean onConnect(INonBlockingConnection nbc) throws IOException, BufferUnderflowException, MaxReadSizeExceededException
      {
          try
          {
              synchronized(sessions)
              {
                  sessions.add(nbc);            
              }
  
              System.out.println("onConnect");
          }
          catch(Exception ex)
          {
              System.out.println("onConnect: " + ex.getMessage());
          }
  
          return true;
      }
  
      public boolean onDisconnect(INonBlockingConnection nbc) throws IOException
      {
          try
          {
              synchronized(sessions)
              {
                  sessions.remove(nbc);            
              }
  
              System.out.println("onDisconnect");
          }
          catch(Exception ex)
          {
              System.out.println("onDisconnect: " + ex.getMessage());
          }        
  
          return true;
      }
  }
  </code></pre>
  
  <p>So, the problem is:</p>
  
  <p>I start the Java Application, and it runs perfectly. I start my Flash application and It runs, creates the socket object, connects to my server (all in my computer) the server gets the connection attempt but, at:</p>
  
  <pre><code>nbc.readStringByDelimiter("\0");
  </code></pre>
  
  <p>it says that nbc is closed, and the server catches the exception: 
  <code>"onData: channel is closed (read buffer size=0)"</code></p>
  
  <p>Does anyone knows why always I get this? Doesn't matter how I write it, use Socket, XMLSocket, xSocketDataHandler, or anyhting, I keep getting the closed channel error.</p>
  
  <p>What am I doing wrong??</p>
  
  <p>Thanks.</p>
  
  <p>EDIT1:</p>
  
  <p>The AS3 Code:</p>
  
  <p>This is my Socket class:</p>
  
  <pre><code>import flash.events.Event;
  import flash.events.IOErrorEvent;
  import flash.events.ProgressEvent;
  import flash.events.SecurityErrorEvent;
  import flash.net.Socket;
  /**
  * ...
  * @author 9MS
  */
  public class GameSocket extends Socket 
  {
      private var host:String;
      private var port:int;
  
      public function GameSocket(host:String="187.127.89.158", port:int=8090)
      {
          super(host, port);
  
          socketConnect();
      }
  
      public function socketConnect():Boolean {
  
  
          addEventListener(Event.CONNECT, onConnect);
          addEventListener(ProgressEvent.SOCKET_DATA, onResponse);
          addEventListener(Event.CLOSE, onClose);
          addEventListener(IOErrorEvent.IO_ERROR, onError);
          addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecError);
  
          try {
              connect(this.host, this.port);
          }
          catch (e:Error) {
              trace("Error on connect: " + e);
              return false;
          }
          return true;
      }
  
      public function sendData(msg:String):void {
          msg += "\0";
  
          try {
              writeUTFBytes(msg);
              flush();
              trace("Message sent: " + msg);
          }
          catch(e:Error) {
              trace("Error sending data: " + e);
          }
  
      }
  
      private function onConnect(e:Event):void {
          trace("Connected: "+e);
      }
  
      private function onResponse(e:ProgressEvent):void {
          trace("Receiving Data: " + e);
  
          var resp:String = readUTFBytes(bytesAvailable);
  
      }
  
      private function onClose(e:Event):void {
          trace("Connection Closed: " + e);
          close();
      }
  
      private function onError(e:IOErrorEvent):void {
          trace("IO Error: " + e.text);
          close();
      }
  
      private function onSecError(e:SecurityErrorEvent):void {
          trace("Security Error: " + e.text);
          close();
      }
  
  }
  </code></pre>