locked
Something strange about the ReadString function

    Question

  • I want to get a message from the stream socket, however, the message's length is not actually equal to the string's length in the stream socket!<img alt="" src="http://social.msdn.microsoft.com/Forums/getfile/459586" />
    Wednesday, May 21, 2014 12:36 PM

Answers

  • Hi,

    Keep reading until we consume the complete stream:

    
    // Once we have written the contents successfully we load the stream.
                    await dataReader.LoadAsync((uint)stream.Size);
    
                    var receivedStrings = "";
    
                    // Keep reading until we consume the complete stream.
                    while (dataReader.UnconsumedBufferLength > 0)
                    {
                        // Note that the call to readString requires a length of "code units" 
                        // to read. This is the reason each string is preceded by its length 
                        // when "on the wire".
                        uint bytesToRead = dataReader.ReadUInt32();
                        receivedStrings += dataReader.ReadString(bytesToRead) + "\n";
                    }
    

    You can refer to the link below:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.datareader.unconsumedbufferlength(v=VS.85).aspx

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.datareader.aspx

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, May 22, 2014 3:42 AM

All replies

  • You have to concatenate the data in the buffers during your loops.

    Let's say you read in 1 byte, because that's all that is in the buffer.  it is shorter than an int, so you repeat, read the next byte, replacing the first you just read.  you're throwing away the first!  the same for the string.  if the buffer has "abcdefg" but should have "abcdefgh" you'll read abcdefg, throw that away and then read h, throw that away, etc...

    I'd make a function that basically performs a while based on the number of bytes/chars you have read, not that are in the communications buffers.

    While collectedbuffer<wantedsize

      read wanteedsize-collectedbuffer.length,

      add to collectedbuffer

    Return collectedbuffer.


    Darin R.

    Wednesday, May 21, 2014 12:47 PM
  • Thank you for your reply very much, I followed your advice and debugged my app, however, I got an exception:The operation attempted to access data outside the valid range(ps: I have used this function to receive other messages successfully, however, I always lost 4 bytes in a particular message, I counldn't find the answer about it, so strange!)
    Wednesday, May 21, 2014 1:07 PM
  • Hi,

    Keep reading until we consume the complete stream:

    
    // Once we have written the contents successfully we load the stream.
                    await dataReader.LoadAsync((uint)stream.Size);
    
                    var receivedStrings = "";
    
                    // Keep reading until we consume the complete stream.
                    while (dataReader.UnconsumedBufferLength > 0)
                    {
                        // Note that the call to readString requires a length of "code units" 
                        // to read. This is the reason each string is preceded by its length 
                        // when "on the wire".
                        uint bytesToRead = dataReader.ReadUInt32();
                        receivedStrings += dataReader.ReadString(bytesToRead) + "\n";
                    }
    

    You can refer to the link below:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.datareader.unconsumedbufferlength(v=VS.85).aspx

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.datareader.aspx

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, May 22, 2014 3:42 AM