login about faq

There are no examples for asynchronous sockets receiving byte data. I can't tell how to identify when such a call has completed.

Within the socket documentation there is a AsyncReceiveBytesN method, but the only way to know how many bytes to pass is to know what the server is sending. For my synchronous calls thus far I have used ReceiveCount easily enough.

    /*
    * Here we send and receive in one blocking call
    */
    public function sendMessage(message : String) : String {

        inUse = true;

        var byteCount : int;

        init();

        Response    = {};
        ResponseStr = '{}';

        if (!Connected())
            return ResponseStr;

        if (Transmit(message)) {

            byteCount = socket.ReceiveCount()

            if (byteCount>0) {

                ResponseStr = socket.ReceiveBytesN(byteCount);

                try {
                    Response = parse(ResponseStr);
                }
                catch (e) {
                    log(socket.LastErrorText)
                }

            }

        }
        lastRecieved = {asString : ResponseStr , asObject : Response}

        inUse = false;

        return ResponseStr;
    }

But with an asynchronous receive, how do I call ReceiveCount without blocking the thread? And when can I call it? It obviously won't be ready when initiating the asynchronous process, but the the process is initiated by socket.AsyncReceiveBytesN()

    /*
    * our outgoing message is relatively small, so we will send
    * that here, the asynch begins on the check for response
    */
    public function Asynch_sendMessage(message : String) : Boolean {    // synchronous

        inUse = true;

        init();

        Response    = {};
        ResponseStr = '{}';

        if (!Connected())
            return false;

        if (Transmit(message)) {

            socket.AsyncReceiveBytesN( ?? );        // I want to allow for receiving N bytes, but the server
                                                    // hasn't processed my request yet, how to I tell the
            return true;                            // component how many bytes we are looking for??
        }

        inUse = false;

        socket.Close();

        return false;

    }

    public function Asynch_checkMessage() : String {                    // asynchronous

        /*
        * basically: check for the message receive to complete
        * and if has, return our information encoded as a string.
        */

        if (socket.AsyncReceiveFinished === 1)

            if (socket.AsyncReceiveSuccess === 1) {

                ResponseStr = decode(socket.AsyncReceivedBytes);

                try {
                    Response = parse(ResponseStr);
                }
                catch (e) {
                    log(socket.LastErrorText);
                }

                lastRecieved = {asString : ResponseStr , asObject : Response};
            }

            inUse = false;

            socket.Close();

            return ResponseStr;
        }

        inUse = true;

        return 'Not Ready';
    }

It seems to me that socket.AsyncReceiveBytesN() cannot be used with a non-constant value for N, but in my server the data is compressed before sending, and therefor the length is not known in advance.

All I can think to do is have the asynchronous method call a different function on the server that will send a string instead that is marked with "-EOM-", but that would counter my efforts to reduce the size with compression...

asked Jan 14 '14 at 13:24

blaze4218's gravatar image

blaze4218
86349


Yes, you could send the byte count in a string. To minimize the size of the string, you could use hex representation of the number, and with the end marked by a single char, such as "." that cannot possibly be a hex digit. For example, to send 500, you would send "1F4."

link

answered Jan 17 '14 at 13:41

chilkat's gravatar image

chilkat ♦♦
11.8k316358420

It looks like you have confused sending the byte count as a string and minimizing the size of the data... By your comment I could either send the byte count (not likely needing compression) as a string i.e "500." and when that is marked finished, initiate another call to send the data as Nbytes, or I can send the data encoded as HEX inflating the compressed output by 2x and terminate that string with a ".". Does that sound right?

(Jan 17 '14 at 17:39) blaze4218

No, I thought that for some odd reason you were worried about the size of the string representation of the message containing the byte count.

(Jan 20 '14 at 11:42) chilkat ♦♦

So, now that we are on the same page- Is their an answer to the original question? Namely: How and where can I use AsyncReceiveBytesN without prior knowledge of N, and does it somehow involve ReceiveCount?

(Jan 23 '14 at 10:23) blaze4218
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×60
×12

Asked: Jan 14 '14 at 13:24

Seen: 6,317 times

Last updated: Jan 23 '14 at 10:23

powered by OSQA