locked
DatagramSocket, DataReader and unconsumedBufferLength property

    Question

  • Hello guys.

    I'm building a really simple app which uses the DatagramSocket to send small text messages between two clients. in fact, I'm doing it all in one app, where I'm instantiating 2 DatagramSockets which listen to different ports to simulate the two clients. Now, I'm only sending and receiving strings. Here's the code I'm using to send a string:

    enviaMsg: function (msg) {
        if (!this.prontoEnviar) return;
        var writer = new Windows.Storage.Streams.DataWriter(this.socket.outputStream);
        writer.writeString(msg);
        writer.storeAsync()
            .done(function () {
                writer.detachBuffer();
            });
    },

    And here's the code I'm using to receive it:

    mensagemRecebida: function (e) {
        var buffer = e.detail[0].getDataReader();
        var numeroChars = buffer.unconsumedBufferLength;
        var str = buffer.readString(numeroChars);
    }

    And now, the question. The docs say that unconsumedBufferLength (http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.datareader.unconsumedbufferlength.aspx) returns the size of the buffer that has not been read, in bytes. But if I'm not mistaken, the JS standard says that each char in a string is stored in 16 bits (ie, 2bytes). That being the, shouldn't the number of chars I recover from the unconsumedBufferLength property equals the number of chars of my string multiplied by 2? My tests show that this is not happening and that when I write a string, the unconsumedBufferLength property will always return the number of chars in that string...Interestingly, writing integers

    I've probably missed something, right? 

    Btw, and since I'm asking about the DataReader class, I've got another question. It introduces readint16, readint32 and readint64. I understand that those make sense in C#, but in JavaScript, aren't all numbers represented by 64 bit floats?? 


    Luis Abreu

    Monday, April 16, 2012 9:20 AM

Answers

  • Hey Luis,

    I have some more info for you on this:

    Why is the “unconsumed buffer length” not reporting # of chars * 2?

    Answer: because there’s the size “on the wire” and the size “in the program”, and they don’t really have any relationship to each other.  In particular, our DataWriter by default will send strings “on the wire” in UTF-8 format.  The DataReader, similarly, will by default read UTF strings.  Naturally, DataWriter will take as a parameter natural JavaScript strings and DataReader will return natural JavaScript strings.

    Why are there the different read/write integer routines for JavaScript?

    Answer:  Because there’s the size “on the wire” and the size “in the program” and they don’t really have any relationship to each other.  Some protocols are specific about exactly how data is to be transmitted (e.g., “there will be a four-byte big-endian two’s complement signed integer followed by size 2-byte big-endian unsigned integers”).  DataWriter and DataReader are designed to help in these cases.  Naturally, DataWriter will take as a parameter normal JavaScript values and DataReader will return normal JavaScript values.


    Jeff Sanders (MSFT)

    Monday, April 23, 2012 12:00 PM
    Moderator

All replies

  • Hi Luis,

    Depending on your character set it could be 1 or 2 bytes.   If you send a unicode string, what is the result?

    -Jeff


    Jeff Sanders (MSFT)

    Tuesday, April 17, 2012 2:54 PM
    Moderator
  • Hello again Jeff.

    can you be more specific? are you talking about the unicodeEncoding property of the datareader and datawriter classes?


    Luis Abreu

    Wednesday, April 18, 2012 9:17 AM
  • Test sending this string: 章容器

    -Jeff


    Jeff Sanders (MSFT)

    Wednesday, April 18, 2012 12:08 PM
    Moderator
  • Hey Luis,

    I have some more info for you on this:

    Why is the “unconsumed buffer length” not reporting # of chars * 2?

    Answer: because there’s the size “on the wire” and the size “in the program”, and they don’t really have any relationship to each other.  In particular, our DataWriter by default will send strings “on the wire” in UTF-8 format.  The DataReader, similarly, will by default read UTF strings.  Naturally, DataWriter will take as a parameter natural JavaScript strings and DataReader will return natural JavaScript strings.

    Why are there the different read/write integer routines for JavaScript?

    Answer:  Because there’s the size “on the wire” and the size “in the program” and they don’t really have any relationship to each other.  Some protocols are specific about exactly how data is to be transmitted (e.g., “there will be a four-byte big-endian two’s complement signed integer followed by size 2-byte big-endian unsigned integers”).  DataWriter and DataReader are designed to help in these cases.  Naturally, DataWriter will take as a parameter normal JavaScript values and DataReader will return normal JavaScript values.


    Jeff Sanders (MSFT)

    Monday, April 23, 2012 12:00 PM
    Moderator
  • Thanks Jeff.

    This makes sense...can't forget that JS is just a language used to develop metro apps...


    Luis Abreu

    Monday, April 23, 2012 12:09 PM