문제

When attempting to download data from a server using StreamSocket, I encounter an error:

An existing connection was forcibly closed

I use a StreamReader to read data from InputStream.AsStreamForRead().

Strangely, this works fine for the first message I download, but throws the above exception for the next message.

The error doesn't happen immediately, it does manage to read most of the message, but at some point it blocks on sr.ReadLine() and then an exception is thrown. See snippet below:

while (true)
{
    str = sr.ReadLine();
    if (str != ".")
    {
        if (str.StartsWith(@".."))
            sb.Append(str.Substring(1) + "\r\n");
        else
            sb.Append(str + "\r\n");
    }
    else break;
}
도움이 되었습니까?

해결책

As it turns out, there was nothing wrong with my code.

My USB dongle modem, kept dropping the connection which resulted in the issue above.

Tested this over a couple of different connections and all is fine.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top