WindowsRT StreamSocket exception when closing connection

2

So I've been getting this exception for about a week now, and I've finally managed to corner it into a code snippet that can be easily read.

As a background, I am programming an app for Windows RT and I am trying to use basic sockets.

For the sake of testing, I've created a local socket listener to act as a server. Both the server and client need to be able to read/write on the socket.

Neither the client nor the server can (or should) know how much data will come across the wire (if any). This is an absolute requirement. The server should be able to process an arbitrary amount of data on demand.

Here is an example. It is presented as a Unit Test, simply because that is where I consistently encounter the error. Removing any single line from this example causes the error to go away:

    [TestMethod]
    public async Task TestSomething()
    {
        //  Setup local server
        //
        StreamSocketListener listener = new StreamSocketListener();
        listener.ConnectionReceived += async (sender, args) =>
        {
            DataReader serverReader = new DataReader(args.Socket.InputStream);
            await serverReader.LoadAsync(4096);   //  <-- Exception on this line
        };

        await listener.BindServiceNameAsync("10181");

        //  Setup client
        //
        using (StreamSocket socket = new StreamSocket())
        {
            await socket.ConnectAsync(new HostName("localhost"), "10181");

            DataReader reader = new DataReader(socket.InputStream);
            Task readTask = Listen(reader);
        }
    }

    public async Task Listen(DataReader reader)
    {
        await reader.LoadAsync(4096);
    }

The exception happens on the line where the server calls LoadAsync(...), and the exception is thrown when the unit test quits.

The exception is (seemingly) simple:

An existing connection was forcibly closed by the remote host. (Exception from HRESULT: 0x80072746)

Any clues would be greatly appreciated.

c#
sockets
async-await
asked on Stack Overflow Dec 18, 2012 by riwalk

1 Answer

4

With the new WinRT socket types, it's easier than ever to program sockets correctly, but make no mistake: they are still complex beasts.

The "forcibly closed" (WSAECONNRESET / 10054) error is when the remote side (in this case, the client) aborts its connection, which it does by disposing its StreamSocket. This is reported as an error but is not uncommon and should be handled gracefully. I.e., if the server has sent all its data and is just waiting to receive more (optional) data, then it should treat WSAECONNRESET as a regular close.

Tip: If you pass Exception.HResult to SocketError.GetStatus, you should see it's SocketErrorStatus.ConnectionResetByPeer. That way you can avoid magic values in your error handling code.

P.S. I have a blog post describing some of the more common socket errors and socket error handling in general.

answered on Stack Overflow Dec 18, 2012 by Stephen Cleary • edited Dec 18, 2012 by Stephen Cleary

User contributions licensed under CC BY-SA 3.0