
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:
Allocate a new SocketAsyncEventArgs object, or get a free one from an application pool.
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).
Call the appropriate socket method to initiate the asynchronous operation.
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).
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.
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.