How to read all the available data from a WinRT StreamSocket and empty the inputStream?


  • I want to read all the data that is currently waiting on a socket before writing new data to it. The reading methods in WinRT are all async so I cannot simply while until the socket is empty. Since I do want to discard the data on the socket, I do not want to use a reader, but straight read the data from the IInputStream of the socket.

    Something like this pseudo code:

    function emptySocket(inputStream, timeout, buffer) {
      if (!buffer) {
        buffer = new Windows.Storage.Streams.Buffer(4096);
      WinJS.Promise.timeout(timeout, inputStream.readAsync(buffer, 4096, Windows.Storage.Streams.InputStreamOptions.partial)
      .then(function(buffer) {
        if (buffer.length === 0) return;
        emptySocket(inputStream, timeout, buffer);
      }), function (error) {

    The timeout is there to break the wait operation when nothing is on the socket any more. Windows.Storage.Streams.InputStreamOptions.partial does not seem to return if nothing is on the socket.

    btw: Whats the difference between Windows.Storage.Streams.InputStreamOptions.partial and Windows.Storage.Streams.InputStreamOptions.readAhead?

    Wednesday, December 17, 2014 7:54 PM

All replies

  • The docs are not very clear about that when to use what option.
    Wednesday, December 17, 2014 7:53 PM
  • Hi pkursawe,

    I think the difference between Partial and ReadAhead is former one read bytes once there is not read bytes income and latter one can pre-define which part of bytes need to be read first when they are ready.

    I will try to consult our seniors to give you some more specific suggestions.



    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.

    Thursday, December 18, 2014 7:26 AM