locked
How to read all pending data from a socket before sending new data

    Question

  • In old win32 times I used called recv until there was nothing left on the socket to read.

    How would I do the same with WinRT? I have a DataReader attached to the inputStream of the TCP socket. The DataReader is set to partial mode. Should I call loadAsync() with a large enough argument to clear all from the socket?

    Thursday, August 7, 2014 10:43 PM

All replies

  • You can check the UnconsumedBufferLength and get that.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, August 11, 2014 12:08 PM
    Moderator
  • No, that property just says how much I have already loaded with the last loadAsync call.

    If I load in 1024 blocks and 2049 bytes are pending my asyn loop would be:

    reader.loadAsync(1024)
    unconsumedBufferLength = 1024
    reader.read(1024)
    reader.loadAsync(1024)
    unconsumedBufferLength = 1024
    reader.read(1024)
    reader.loadAsync(1024)
    unconsumedBufferLength = 1
    reader.loadAsync(1024) -> would block now, because there are no more bytes to load

    But I would like to read everything from the socket before I send new data to it. So that when thex next reply arrives I do not have old data on the sockers InputStream waiting.

    Monday, August 11, 2014 12:31 PM