Working with Sockets

Microsoft Silverlight will reach end of support after October 2021. Learn more.

The System.Net.Sockets namespace in Silverlight provides a managed implementation of the sockets networking interface for developers who need to tightly control access to the network. On Windows, the System.Net.Sockets namespace provides a managed implementation of the Windows Sockets (Winsock) interface. On Apple Mac OS X, the System.Net.Sockets namespace provides a managed implementation of the sockets interface based on Berkeley Software Distribution (BSD) UNIX.

The System.Net.Sockets namespace provides a mechanism for real-time duplex communication with remote network resources and enables higher-level APIs to communicate over a bi-directional transport. This also allows an application to interoperate as a client with existing TCP services.

The System.Net.Sockets namespace in Silverlight 4 has added support for UDP multicast clients. For more information, see Working with Multicast.

Silverlight for Windows Phone For additional socket methods for Windows Phone, see SocketExtensions.

Networking Protocol Support

The System.Net.Sockets namespace supports the use of IPv4 or IPv6 protocols as long as networking on the local computer has support enabled for IPv4 and IPv6. Additional classes were added to the System.Net namespace in Silverlight 2 and later to work with System.Net.Sockets. These new classes include the following:

  • DnsEndPoint - Represents a network endpoint as a host name or a string representation of an IP address and a port number.

  • EndPoint - Identifies a network address. This is an abstract class.

  • IPAddress - Provides an Internet Protocol (IP) address.

  • IPEndPoint - Represents a network endpoint as an IP address and a port number.

  • SocketAddress - Stores serialized information from EndPoint derived classes.

The Socket class provides a set of methods and properties for network communications. The Socket class allows you to perform asynchronous data transfer using any of the communication protocols listed in the ProtocolType enumeration.

For Silverlight for the desktop, the only supported ProtocolType is the TCP protocol. For Silverlight for the desktop, TCP unicast and UDP multicast clients are supported.

Silverlight for Windows Phone For Windows Phone OS 7.1, the supported ProtocolType values are the TCP and UDP protocol. For Windows Phone OS 7.1, TCP unicast, UDP unicast, and UDP multicast clients are supported.

One restriction on using sockets in Silverlight is that the port range that a network application is allowed to connect to must be within the range of 4502-4534. These are the only ports allowed for connection using sockets from Silverlight applications. If a connection is to a port is not within this port range, the connection attempt will fail.

Basic Network Programming with Sockets

The Socket class allows you to perform asynchronous data transfer using the following methods:

  • ConnectAsync - Starts an asynchronous request for a connection to the remote host.

  • SendAsync - Writes outgoing data from one or more buffers on a connected socket.

  • ReceiveAsync - Reads incoming data into one or more buffers from a connected socket.

  • Shutdown - Finishes any pending send operations, and signals the remote endpoint that the connection should be closed. If Send is specified, data may still be received until the remote computer closes its end of the connection (indicated by receiving 0 bytes).

  • Close - Closes the remote host connection and releases all managed and unmanaged resources associated with the socket.

In the Socket class, asynchronous socket operations are described by reusable System.Net.Sockets.SocketAsyncEventArgs objects allocated and maintained by the application. The application can create as many of the SocketAsyncEventArgs objects that it needs. For example, if a Silverlight application needs to have 10 socket send operations outstanding at the same time, it can allocate 10 reusable SocketAsyncEventArgs objects in advance for that purpose.

The pattern for performing an asynchronous socket operation consists of the following steps:

  1. Allocate a new SocketAsyncEventArgs object, or get a free one from an application pool.

  2. Set properties on the SocketAsyncEventArgs object required for the operation about to be performed (the event handler attached to the Completed event and the RemoteEndPoint property for the ConnectAsync method, for example).

  3. Call the appropriate socket method to initiate the asynchronous operation.

  4. If the asynchronous socket method returns true, the I/O operation is pending. The SocketAsyncEventArgs.Completed event on the SocketAsyncEventArgs object passed to the socket method will be raised upon completion of the operation. When the event is raised in the event handler, query the SocketAsyncEventArgs properties for the completion status and socket operation results (the amount of received data in the buffer for the ReceiveAsync method call, for example).

  5. If the asynchronous socket method returns false, the operation completed synchronously. The SocketAsyncEventArgs properties may be queried for the completion status and socket operation results.

  6. Reuse the SocketAsyncEventArgs for another operation, put it back in the pool, or discard it.

The lifetime of the new SocketAsyncEventArgs object used in an asynchronous socket operation is determined by references in the application code and asynchronous I/O references. It is not necessary for the application to retain a reference to the SocketAsyncEventArgs object after it is submitted as a parameter to one of the asynchronous socket methods. It will remain referenced until the completion callback returns. However, it is advantageous for the application to retain the reference to the SocketAsyncEventArgs object so that it can be reused for a future asynchronous socket operation.

When you are finished sending and receiving data, use the Shutdown method to disable the Socket. After calling Shutdown and optionally receiving confirmation that the remote endpoint has also shutdown the connection via a receive operation that gets 0 bytes, call the Close method to release all resources associated with the Socket.

Security Policy Restrictions on Connecting to Sites

The security policy system in the Silverlight 3 runtime requires that a policy file be downloaded from a network resource before a network connection is allowed to access that resource. This affects both site-of-origin and cross-domain network access. For more information on the security policy system in Silverlight, see Network Security Access Restrictions in Silverlight.

When connecting to the site-of-origin, the browser only exposes the hostname used to download the application and not the IP address. The DnsEndPoint class can be used to represent a hostname string or an IP address.

One method that can be used to determine the host name of the site-of-origin is to use the System.Windows.Application class in the System.Windows namespace to get the Application.Current property of the Silverlight application. This property returns a System.Windows.Interop.SilverlightHost instance for the Silverlight application. The SilverlightHost.Source property on this instance gets the Uri used to connect to the host. The Uri.Host property gets the host component of this instance. This host component can then be used with a port number to construct a new DnsEndPoint instance by calling one of the DnsEndPoint constructors.

The SocketAsyncEventArgs.RemoteEndPoint property on the SocketAsyncEventArgs instance specifies the remote endpoint to which to connect using the ConnectAsync method. The DnsEndPoint instance created using the SilverlightHost.Source property is then used to set the SocketAsyncEventArgs.RemoteEndPoint property to the site or host of origin before calling the ConnectAsync method to establish a connection.