DataReader result truncated when reading from Stre

2019-05-19 21:57发布

问题:

I have the following c# / WinRT code to receive a response from an IMAP command

DataReader reader = new DataReader(sock.InputStream);
reader.InputStreamOptions = InputStreamOptions.Partial;
await reader.LoadAsync(1000000);
string data = string.Empty;
while(reader.UnconsumedBufferLength > 0){
    data += reader.ReadString(reader.UnconsumedBufferLength);
}

The results are truncated after 8192 bytes. 8192 looks suspiciously like a limit of some kind. How do I get around this?

回答1:

The abstraction of TCP/IP sockets is a stream of bytes, not a stream of messages, as I explain on my blog. So it's perfectly normal to have a partial LoadAsync return after a partial message or any number of messages.

8192 sounds like it could be a jumbo frame (Gigabit Ethernet). Or perhaps the default read size of some .NET layer.

Anyway, what you're seeing is perfectly normal and acceptable for TCP/IP communications. You have to loop on LoadAsync (and ReadString) until you detect the end of your message. I think IMAP uses linefeeds, at least for some of its message delimeters. Normally, you'd have to handle situations where your message would end in the middle of a string, but I think may might be able to skip this check for IMAP.