3 Replies Latest reply on Nov 29, 2006 1:20 PM by ntsiii

    XMLSocket Issue


      I have written a small program which uses XMLSocket to communicate with some server.
      It works for the first time, I mean when i send first message, I got the response from server but after that flex client does not send any message to server and i see no exception in the code.

      can anybody pls. help ?

      Following is the code:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" width="800" height="600">
      <mx:TraceTarget level="0" />

      import asclasses.SocketComm;
      import flash.system.Security;
      import asclasses.ACTURLLoader;
      import mx.controls.Alert;
      import mx.rpc.events.ResultEvent;
      import mx.rpc.events.FaultEvent;
      import mx.rpc.http.HTTPService;

      private var response:String;
      private var socketComm:SocketComm;
      private var sock:XMLSocket;
      private var isSocketConnected:Boolean = false;

      private function handleTest():void {

      // Printing Sandbox
      var sandbox:String = Security.sandboxType;
      sandboxType.text = sandbox;

      if(!isSocketConnected) {
      // Local Windows Service
      var host:String = winServiceHost.text;
      var port:String = winServicePort.text;
      socketComm = new SocketComm();
      sock = socketComm.createSocketConnection(host, int(port));
      }else {
      Alert.show("Entered in else part");
      if(sock.connected) {
      Alert.show("socket is connected");
      try {
      }catch(ie:IOError) {
      }else {
      Alert.show("sock is not conected");


      private function configureSocketListeners(dispatcher:IEventDispatcher):void {
      dispatcher.addEventListener(Event.CLOSE, socketHandler);
      dispatcher.addEventListener(Event.CONNECT, socketHandler);
      dispatcher.addEventListener(DataEvent.DATA, socketHandler);
      dispatcher.addEventListener(ErrorEvent.ERROR, socketHandler);
      dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, socketHandler);
      dispatcher.addEventListener(IOErrorEvent.IO_ERROR, socketHandler);
      dispatcher.addEventListener(ProgressEvent.SOCKET_DATA, socketHandler);

      private function socketHandler(event:Event):void {
      var socket:XMLSocket = XMLSocket(event.target);

      if(event.type == Event.CLOSE) {
      winServiceContent.text = response;
      }else if(event.type == SecurityErrorEvent.SECURITY_ERROR) {
      }else if(event.type == IOErrorEvent.IO_ERROR) {
      }else if(event.type == DataEvent.DATA) {
      winServiceContent.text += DataEvent(event).data;
      }else if(event.type == Event.CONNECT) {
      // Sending first msg
      Alert.show("Sending Message : " + msg.text);
      isSocketConnected = true;
      }else if(event.type == ProgressEvent.SOCKET_DATA) {
      trace("progressHandler loaded:" + ProgressEvent(event).bytesLoaded + " total: " + ProgressEvent(event).bytesTotal);

      <mx:Form id="fff" x="36" y="10" width="500" height="414">
      <mx:FormHeading label="Security Test Form"/>
      <mx:FormItem label="Sandbox Type">
      <mx:Text id="sandboxType"/>
      <mx:FormItem label="Windows Service Host">
      <mx:TextInput id="winServiceHost" text=""/>
      <mx:FormItem label="Windows Service Port">
      <mx:TextInput id="winServicePort" text="8081"/>
      <mx:FormItem label="Command Message">
      <mx:TextInput id="msg" />
      <mx:FormItem label="Windows Service Content">
      <mx:TextArea id="winServiceContent"/>
      <mx:Button label="Test" click="handleTest()" x="54" y="452"/>
      <mx:Button label="Clear" x="132" y="452" />

        • 1. Re: XMLSocket Issue
          ntsiii Level 3
          What is "SocketComm"?
          • 2. Re: XMLSocket Issue
            tarun_kar Level 1
            Hi Tracy,

            Thanks for your reply.
            SocketComm is a action script class which maintains XMLSocket object for the whole application.

            package asclasses
            import flash.display.Sprite;
            import flash.events.*;
            import flash.net.XMLSocket;
            import mx.controls.Alert;

            public class SocketComm extends Sprite
            private var hostName:String = "localhost";
            private var port:uint = 8080;
            private var socket:XMLSocket;

            public function createSocketConnection(hostName:String, port:uint):XMLSocket {
            if(socket == null) {
            socket = new XMLSocket();
            socket.connect(hostName, port);
            }else if(socket != null && !socket.connected) {
            socket.connect(hostName, port);
            return socket;

            Actually now i am able to communicate with server over XMLSocket.
            There was some problem at server side.

            But still i am not comfortable with using SocketComm(XMLSocket) as a common utility class due to event based asynchronus model. Like in Java Socket all calls are synchronus, I can easily create common class for socket communication.

            But due to my unfamiliarity with event based asynchrous model of programming, I am facing difficulties.

            Like my requirement is to use SocketComm class as a communication medium with server for various business operations like add, modify and delete etc. from various Flex UI screens. But due to event based model, I am not really sure how and when socket is going to send response.

            So if you look at my first post, I have added all listeners to mxml page but i feel this is not a good design, As i have atleast 4-5 mxml pages where in I will repeat these listeners.

            I would really appreciate if you could provide me some guidance on using SocketComm class as a effective common class.

            Thanks in advance :)


            • 3. Re: XMLSocket Issue
              ntsiii Level 3
              I am using XMLSocket in one of my Flex 1.5 apps and I need to be able touase it in Flex 2, so I plan to work with your example.

              Be patient though, I don't know when I'll get to work with it.