7 Replies Latest reply on Jul 20, 2011 8:43 AM by yyterrwihbgd

    Problem with socket responses with flex

    yyterrwihbgd

      Hi, I am using the code below to connect to an IMAP server.

      When I telnet to the server and use the commands that I am sending in the code, the rsponses are correct.

       

      However, when I run the code below, I only obtain the ready and logged in responses as shown below.

       

      Any ideas why I am not receiving the full set of responses from the socket?

      Thanks in advance fro your advice.

       

      The following is the output from the code:


      +++++++++++++++++ START SENDING IMAP DATA +++++++++++++++++++++++++

      sent: . login user1 myPassword


      sent: . status INBOX (messages)


      sent: . select INBOX


      +++++++++++++++++ END SENDING IMAP DATA +++++++++++++++++++++++++


      ++++++++ [IMAP START]

      Response is: * OK Dovecot ready.

      [IMAP END] +++++++++


      ++++++++ [IMAP START]

      Response is: . OK Logged in.

      [IMAP END] +++++++++

       

       

      The code is:

       

      <?xml version="1.0" encoding="utf-8"?>

      <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"

         xmlns:s="library://ns.adobe.com/flex/spark"

         xmlns:mx="library://ns.adobe.com/flex/mx"

         applicationComplete="init()">

      <fx:Declarations>

      <!-- Place non-visual elements (e.g., services, value objects) here -->

      </fx:Declarations>

       

       

      <fx:Script>

      <![CDATA[

       

      public var dataStr:String;

      private var socket:Socket;

      private var serverURL:String= "dead.org";

      private var serverPort:int = 143;

      private var serverResponse:ByteArray = new ByteArray();

       

      private function init():void

          {

        this.serverURL = serverURL;

        this.serverPort = serverPort;

        socket = new Socket();

        socket.addEventListener(ProgressEvent.SOCKET_DATA,handleNewIMAPData); //Event when socket receives new data

        this.connectToServer();

       

        sendIMAPdata();


      }

       

      private function sendString(dataStr:String):void

      {

        var bytes:ByteArray = new ByteArray();

        bytes.writeMultiByte(dataStr, "UTF-8");

        socket.writeBytes(bytes);

        socket.flush();

        trace("sent: " + dataStr);

      }

       

      public function sendIMAPdata():void

      {

        trace("\t +++++++++++++++++ START SENDING IMAP DATA +++++++++++++++++++++++++");

        dataStr =". login user1 myPassword" + "\r\n";

        sendString(dataStr);

        dataStr =". status INBOX (messages)" + "\r\n";

        sendString(dataStr);

        dataStr =". select INBOX" + "\r\n";

            sendString(dataStr);

        trace("\t +++++++++++++++++ END SENDING IMAP DATA +++++++++++++++++++++++++");

      }

       

      private function handleNewIMAPData(event:ProgressEvent):void

      {

        var numBytes:int = socket.bytesAvailable;

        serverResponse = new ByteArray();

       

        while(socket.bytesAvailable)

        {

          var byte:int = socket.readUnsignedByte();

      serverResponse.writeByte(byte);

        }

       

        var response:String = serverResponse.toString();

        trace(" ++++++++ [IMAP START]\n Response is: " + response + "[IMAP END] +++++++++\n");

      }

       

      private function connectToServer():void

      {

        socket.connect(serverURL, serverPort);

      }

       

      ]]>

      </fx:Script>

       

      </s:WindowedApplication>