none
[UWP][C#] How to read from a StreamSocket's stream and return with available bytes immediately? RRS feed

  • Question

  • StreamSocket streamSocketGet = new StreamSocket();
    streamSocketGet.Control.QualityOfService = SocketQualityOfService.LowLatency;
    await streamSocketGet.ConnectAsync(endpointPair);
    Stream stream = streamSocketGet.InputStream.AsStreamForRead();
    int iRead = await stream.ReadAsync(abBuffer, _iBufferIndex, iByteToRead);

    Under some circumstances, it takes a long time for ReadAsync() to return.  For example,  it may take 3 seconds to return 35K bytes.  I know the network packet size is about 1.5K bytes. 

    The following is from the official document for ReadAsync():

    "The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if the end of the stream has been reached."

    It gives me the impression that the method is supposed to return immediately with currently available bytes. 

    Could anyone she some light on this?



    Hong

    Wednesday, September 4, 2019 3:58 PM

Answers

  • Hi,

    You could try to read the bytes in chunks and the recommended way is to use ReadAsync(Byte[], Int32, Int32) method and define to read the data in smaller numbers.

    Best Regards,

    Fay


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Hong (MA, USA) Friday, September 13, 2019 3:54 AM
    Friday, September 13, 2019 2:53 AM

All replies

  • Hi,

    "The value of the TResult parameter contains the total number of bytes read into the buffer. The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if the end of the stream has been reached."

    I think it doesn't mean the return value will return immediately if the value number is less than the request number. The key point of it is telling us that sometimes the return value is less than the requested value. For example, you request about 30K but there is only 10K available, so you will only get 10k. But the time you could receive them also depends on your network.Network status has a big impact on the data transport process between the server and you app even when them are in the same local network. 

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Thursday, September 5, 2019 3:10 AM
    Moderator
  • Thank you for the elucidation. 

    Do you know how the method decides to return with data for usage in video, audio streaming that has endless data, but the data rate is fair stable?  I cannot figure out a pattern. Sometimes it returns immediately, but sometimes it takes a long time to return a lot of data.  I assume the socket has some buffer. Is there a way to just fetch the data in the buffer and return immediately?


    Hong

    Thursday, September 5, 2019 3:27 AM
  • Hi,

    I searched about such API and also checked the document about such API that could directly read the buffer first. But I could not find one that seems to be possible to do that. So I'll try to ask another engineer to take a look at this issue. There might be some time delay. Thank you for your patience. 

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 6, 2019 3:12 AM
    Moderator
  • Thanks a lot for looking into this.

    Hong

    Friday, September 6, 2019 3:24 AM
  • Hi,

    You could try to read the bytes in chunks and the recommended way is to use ReadAsync(Byte[], Int32, Int32) method and define to read the data in smaller numbers.

    Best Regards,

    Fay


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Hong (MA, USA) Friday, September 13, 2019 3:54 AM
    Friday, September 13, 2019 2:53 AM