b.pell
b.pell

Reputation: 4288

Get a StreamSocket's Connection Status (UWP)

I'm using a StreamSocket in a Windows Universal app to make a TCP/IP connection to a text based game (the socket is left open for an extended period of time). I have it connecting and returning data fine. Most socket implementations I've used in the past have a property or function (or a way to poll the socket) and get the status (whether it's open, closed, broken, pending, etc.).

I haven't been able to find a way to do this with the StreamSocket.

My question is, How do I query the status of the StreamSocket connection to see if it's dropped (without waiting until I send something down the pipe for it to error out)?

E.g., something so I could do something like:

if (this.Socket.Status != Status.Connected)...

Upvotes: 1

Views: 1157

Answers (1)

Nico Zhu
Nico Zhu

Reputation: 32775

if (this.Socket.Status != Status.Connected)

The code you wrote was quite elegant , however, as far as I know there is no such api provided by uwp.

The Windows.Networking.Sockets namespace has convenient helper methods and enumerations for handling errors when using sockets and WebSockets. This can be useful for handling specific network exceptions differently in your app.

An error encountered on DatagramSocket, StreamSocket, or StreamSocketListener operation is returned as an HRESULT value. The SocketError.GetStatus method is used to convert a network error from a socket operation to a SocketErrorStatus enumeration value. Most of the SocketErrorStatus enumeration values correspond to an error returned by the native Windows sockets operation. An app can filter on specific SocketErrorStatus enumeration values to modify app behavior depending on the cause of the exception.

For parameter validation errors, an app can also use the HRESULT from the exception to learn more detailed information on the error that caused the exception. Possible HRESULT values are listed in the Winerror.h header file. For most parameter validation errors, the HRESULT returned is E_INVALIDARG .

Upvotes: 3

Related Questions