次の方法で共有


How to set timeouts on socket operations (XAML)

This topic shows how to set timeouts on network socket operations in a Windows Runtime app to limit the time to wait for an operation to complete.

What you need to know

Technologies

Prerequisites

  • The following information applies to any connected or network-aware Windows Runtime app that uses sockets for network connections. This topic applies to apps written in C++/XAML and apps using the .NET Framework 4.5 in C#, VB.NET, or managed C++ on Windows 8.1, Windows Phone 8.1, and Windows Server 2012 R2.

  • The following examples in this topic are provided in C#. A basic understanding of sockets is recommended.

Instructions

Step 1: Overview of default timeouts on socket operations

The StreamSocket class implements a TCP socket in a Windows Runtime app. A TCP socket must establish a connection before any network data can be sent or received. The underlying TCP socket implementation in Windows 8.1, Windows Phone 8.1, and Windows Server 2012 R2 sets a default timeout on any TCP connect socket operations. The default timeout is 3 minutes (180 seconds) for each source and destination address pair when hostnames or endpoints are used. So if a destination hostname has two IP addresses, then the connect operation would not timeout until about 6 minutes have elapsed. This default timeout may well be excessive for the customer experience using a Windows Runtime app. So an app using the StreamSocket class may want to set a shorter custom timeout on stream socket connect operations.

The DatagramSocket and StreamSocket classes do not have any default timeouts when sending or receiving network data. So any send or receive operation will wait forever. A Windows Store app using sockets may want to set a timeout on these operations for a better customer experience.

The StreamSocketListener class will listen and wait forever for incoming connection requests.

Step 2: How to set custom timeouts on socket operations

The .NET Framework 4.5 implements a CancellationTokenSource class in the System.Threading namespace that can be used with a timeout. A socket operation can be run as a task with a CancellationTokenSource object to implement a timeout.

The basic model to use timeouts is the same for all three classes. The discussion below uses a connect operation on a StreamSocket as an example. The same model can be used to implement timeouts when sending or receiving network data with the DatagramSocket or StreamSocket object or when listening for incoming connections with a StreamSocketListener object.

  • Create a StreamSocket.
  • Create a CancellationTokenSource and set a timeout on the token.
  • Run the socket operation on the StreamSocket as a task with the CancellationTokenSource object associated with the task.
  • If the request fails with a TaskCanceledException exception, the socket operation timed out with our custom timeout. Close the socket to cancel the operation.

Once a socket operation is cancelled, the socket can no longer be used.

The following sample implements a custom timeout on a StreamSocket connect operation.


StreamSocket clientSocket = new StreamSocket();
 
HostName host = new HostName("www.contoso.com");
string port = "http";

CancellationTokenSource cts = new CancellationTokenSource();
 
try
{
    cts.CancelAfter(10000);
    await clientSocket.ConnectAsync(host, port).AsTask(cts.Token);
}
catch (TaskCanceledException)
{
    clientSocket.Close();
    // Debug.WriteLine("Operation was cancelled.");
}

Other

Cancellation in Managed Threads

Connecting with sockets

How to connect with a datagram socket

How to connect with a stream socket

How to secure socket connections with TLS/SSL

Reference

CancellationTokenSource

DatagramSocket

StreamSocket

StreamSocketListener

System.Threading

Windows.Networking.Sockets