0 Replies Latest reply on Mar 7, 2008 5:33 AM by Sunilnairg

    Receiving data through TCP Socket

    Sunilnairg
      Hi all,

      I am trying to receive some data through TCP Socket from the server using XMLSocketClass. Ther server is responding with some data. But I can't access this data in my application. Pls tell me the reasons for not working of handler private function dataHandler(event:DataEvent):void .

      =============================================================================

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
      <mx:Script>
      <![CDATA[
      import mx.controls.Alert;
      import mx.collections.ArrayCollection;
      import flash.display.Sprite;
      import flash.events.Event;
      import flash.events.DataEvent;
      import flash.events.IOErrorEvent;
      import flash.net.XMLSocket;
      private var socket:XMLSocket;
      private var nextId:int;
      private var events:ArrayCollection = new ArrayCollection();
      public static var host:String = "34.234.43.97";
      public static var port:Number = 8002;
      public var xml:XML;

      private function connectToServer():void
      {
      socket = new XMLSocket();
      socket.addEventListener(DataEvent.DATA, dataHandler);
      configureListeners(socket);
      socket.connect(host, port);

      }
      //This function is Not working

      private function dataHandler(event:DataEvent):void {
      Alert.show("dataHandler: " + event.data);
      xml = new XML(event.data);
      Alert.show(xml); }


      private function configureListeners(dispatcher:IEventDispatcher):void {

      dispatcher.addEventListener(Event.CLOSE, closeHandler);

      dispatcher.addEventListener(Event.CONNECT, connectHandler);

      dispatcher.addEventListener(DataEvent.DATA, dataHandler);

      dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

      dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);

      dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
      }
      private function closeHandler(event:Event):void {
      trace("closeHandler: " + event);
      }

      private function ioErrorHandler(event:IOErrorEvent):void {
      trace("ioErrorHandler: " + event);
      }

      private function progressHandler(event:ProgressEvent):void {
      trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
      }

      private function securityErrorHandler(event:SecurityErrorEvent):void {
      trace("securityErrorHandler: " + event);
      }
      /* private function dataHandler(event:DataEvent):void {
      trace("dataHandler: " + event);
      } */

      private function connectHandler(event:Event):void
      {
      var obj:Object = new Object();
      obj.id = nextId++;
      obj.eventName="connect";
      obj.timestamp = new Date().valueOf();
      events.addItem(obj);
      }
      private function sendData():void
      {
      var xmlvalue:String=txtData.text.toString() ;
      var xmlfile:String = "<command>SndIns<parameter1>0x06</parameter1><parameter2>0x00</parameter2><parameter3>0x7 1</parameter3><parameter4>0x0F</parameter4><parameter5>0x11</parameter5><parameter6>0xFF</ parameter6></command>";
      socket.send(xmlfile);
      Alert.show(xmlfile);

      }
      ]]>
      </mx:Script>
      <mx:HBox width="80%" horizontalAlign="center">
      <mx:TextInput id="txtData" name=""/>
      <mx:Button id="btnConnect" label="Connect" click="connectToServer();btnConnect.enabled = false"/>
      <mx:Button id="btnSend" label="Send Data" click="sendData()"/>
      <!--<mx:Button label="getData" id="btnGet" click="getData()"/>-->
      </mx:HBox>
      <mx:HBox x="10" y="30" width="100%">
      <mx:DataGrid width="80%" height="100%" dataProvider="{xml}">
      <mx:columns>
      <mx:Array>
      <mx:DataGridColumn headerText="Event Name" dataField="eventName"/>
      </mx:Array>
      </mx:columns>
      </mx:DataGrid>
      </mx:HBox>
      </mx:Application>