SocketAsyncEventArgs Class
Represents an asynchronous socket operation.
Assembly: System (in System.dll)
The SocketAsyncEventArgs type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | AcceptSocket | Gets or sets the socket to use or the socket created for accepting a connection with an asynchronous socket method. |
![]() | Buffer | Gets the data buffer to use with an asynchronous socket method. |
![]() | BufferList | Gets or sets an array of data buffers to use with an asynchronous socket method. |
![]() | BytesTransferred | Gets the number of bytes transferred in the socket operation. |
![]() | ConnectByNameError | Gets the exception in the case of a connection failure when a DnsEndPoint was used. |
![]() | ConnectSocket | The created and connected Socket object after successful completion of the ConnectAsync method. |
![]() | Count | Gets the maximum amount of data, in bytes, to send or receive in an asynchronous operation. |
![]() | DisconnectReuseSocket | Gets or sets a value that specifies if socket can be reused after a disconnect operation. |
![]() | LastOperation | Gets the type of socket operation most recently performed with this context object. |
![]() | Offset | Gets the offset, in bytes, into the data buffer referenced by the Buffer property. |
![]() | ReceiveMessageFromPacketInfo | Gets the IP address and interface of a received packet. |
![]() | RemoteEndPoint | Gets or sets the remote IP endpoint for an asynchronous operation. |
![]() | SendPacketsElements | Gets or sets an array of buffers to be sent for an asynchronous operation used by the Socket::SendPacketsAsync method. |
![]() | SendPacketsFlags | Gets or sets a bitwise combination of TransmitFileOptions values for an asynchronous operation used by the Socket::SendPacketsAsync method. |
![]() | SendPacketsSendSize | Gets or sets the size, in bytes, of the data block used in the send operation. |
![]() | SocketError | Gets or sets the result of the asynchronous socket operation. |
![]() | SocketFlags | Gets the results of an asynchronous socket operation or sets the behavior of an asynchronous operation. |
![]() | UserToken | Gets or sets a user or application object associated with this asynchronous socket operation. |
| Name | Description | |
|---|---|---|
![]() | Dispose | Releases the unmanaged resources used by the SocketAsyncEventArgs instance and optionally disposes of the managed resources. |
![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() | Finalize | Frees resources used by the SocketAsyncEventArgs class. (Overrides Object::Finalize().) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | OnCompleted | Represents a method that is called when an asynchronous operation completes. |
![]() | SetBuffer(Int32, Int32) | Sets the data buffer to use with an asynchronous socket method. |
![]() | SetBuffer(array<Byte>, Int32, Int32) | Sets the data buffer to use with an asynchronous socket method. |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
The SocketAsyncEventArgs class is part of a set of enhancements to the System.Net.Sockets::Socket class that provide an alternative asynchronous pattern that can be used by specialized high-performance socket applications. This class was specifically designed for network server applications that require high performance. An application can use the enhanced asynchronous pattern exclusively or only in targeted hot areas (for example, when receiving large amounts of data).
The main feature of these enhancements is the avoidance of the repeated allocation and synchronization of objects during high-volume asynchronous socket I/O. The Begin/End design pattern currently implemented by the System.Net.Sockets::Socket class requires a System::IAsyncResult object be allocated for each asynchronous socket operation.
In the new System.Net.Sockets::Socket class enhancements, asynchronous socket operations are described by reusable SocketAsyncEventArgs objects allocated and maintained by the application. High-performance socket applications know best the amount of overlapped socket operations that must be sustained. The application can create as many of the SocketAsyncEventArgs objects that it needs. For example, if a server application needs to have 15 socket accept operations outstanding at all times to support incoming client connection rates, it can allocate 15 reusable SocketAsyncEventArgs objects for that purpose.
The pattern for performing an asynchronous socket operation with this class consists of the following steps:
Allocate a new SocketAsyncEventArgs context object, or get a free one from an application pool.
Set properties on the context object to the operation about to be performed (the completion callback method, the data buffer, the offset into the buffer, and the maximum amount of data to transfer, for example).
Call the appropriate socket method (xxxAsync) to initiate the asynchronous operation.
If the asynchronous socket method (xxxAsync) returns true, in the callback, query the context properties for completion status.
If the asynchronous socket method (xxxAsync) returns false, the operation completed synchronously. The context properties may be queried for the operation result.
Reuse the context for another operation, put it back in the pool, or discard it.
The lifetime of the new asynchronous socket operation context object is determined by references by the application code and asynchronous I/O references. It is not necessary for the application to retain a reference to an asynchronous socket operation context object after it is submitted as a parameter to one of the asynchronous socket operation methods. It will remain referenced until the completion callback returns. However it is advantageous for the application to retain the reference to the context so that it can be reused for a future asynchronous socket operation.
The following code example implements the connection logic for the socket server that uses the SocketAsyncEventArgs class. After accepting a connection, all data read from the client is sent back to the client. The read and echo back to the client pattern is continued until the client disconnects. The BufferManager class that is used by this example is displayed in the code example for the SetBuffer(array<Byte>, Int32, Int32) method. The SocketAsyncEventArgsPool class that is used in this example is displayed in the code example for the SocketAsyncEventArgs constructor.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
