Question

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;
}
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top