Windows UWP StreamSocket ReOpen

0

Hey, I have a Problem with the Windows 10 UWP API.

I'm developing a Windows 10 UWP App and need to connect to a Chromecast. I'm using SharpCaster for this. But when I open a connection to a Chromecast and close it again later on, it is not possible to connect to a Chromecast again. The socket to the Chromecast opens again, but when trying to write to it, I get the following exception:

A method was called at an unexpected time. (Exception from HRESULT: 0x8000000E)

This even happens when I turn the Chromecast off while disconnected. I disconnect the Chromecast with this Method:

public void Disconnect()
{
    _running = false;
    _socket.InputStream.Dispose();
    _socket.OutputStream.Dispose();
    _socket.Dispose();
}

The method is not found in the Library, I have written it myself. Setting _running to false stops all the loops for pinging, etc...

The socket is created with this code:

_socket = new StreamSocket().ConfigureForChromecast();
await _socket.ConnectAsync(new HostName(uri.Host), ChromecastPort, SocketProtectionLevel.Tls10);

The extension ConfigureForChromecast() looks like this:

public static StreamSocket ConfigureForChromecast(this StreamSocket socket)
{
    //Chromecast is not using trusted certificate so ignoring errors caused by that
    socket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);
    socket.Control.IgnorableServerCertificateErrors.Add(ChainValidationResult.InvalidName);
    socket.Control.OutboundBufferSizeInBytes = 2048;

    socket.Control.KeepAlive = true;
    socket.Control.QualityOfService = SocketQualityOfService.LowLatency;
    return socket;
}

Finally, the messages are written to the socket with

internal async Task Write(byte[] bytes)
{
    try
    {
        var buffer = CryptographicBuffer.CreateFromByteArray(bytes);
        await _socket.OutputStream.WriteAsync(buffer);
    }
    catch (Exception e)
    {
        Debugger.Break();
    }

}

And that is the point where the exception occurs. When connecting the first time, it works perfectly, but to connect a second time, I have to restart the whole app. Any ideas why?

c#
.net
sockets
uwp
chromecast
asked on Stack Overflow Jul 21, 2016 by Luca Schimweg

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0