This documentation is archived and is not being maintained.

SocketAsyncEventArgs Class

Represents an asynchronous socket operation.

System::Object
  System::EventArgs
    System.Net.Sockets::SocketAsyncEventArgs

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)

public ref class SocketAsyncEventArgs : public EventArgs, 
	IDisposable

The SocketAsyncEventArgs type exposes the following members.

  NameDescription
Public methodSocketAsyncEventArgsCreates an empty SocketAsyncEventArgs instance.
Top

  NameDescription
Public propertyAcceptSocketGets or sets the socket to use or the socket created for accepting a connection with an asynchronous socket method.
Public propertyBufferGets the data buffer to use with an asynchronous socket method.
Public propertyBufferListGets or sets an array of data buffers to use with an asynchronous socket method.
Public propertyBytesTransferredGets the number of bytes transferred in the socket operation.
Public propertyConnectByNameErrorGets the exception in the case of a connection failure when a DnsEndPoint was used.
Public propertyConnectSocketThe created and connected Socket object after successful completion of the ConnectAsync method.
Public propertyCountGets the maximum amount of data, in bytes, to send or receive in an asynchronous operation.
Public propertyDisconnectReuseSocketGets or sets a value that specifies if socket can be reused after a disconnect operation.
Public propertyLastOperationGets the type of socket operation most recently performed with this context object.
Public propertyOffsetGets the offset, in bytes, into the data buffer referenced by the Buffer property.
Public propertyReceiveMessageFromPacketInfoGets the IP address and interface of a received packet.
Public propertyRemoteEndPointGets or sets the remote IP endpoint for an asynchronous operation.
Public propertySendPacketsElementsGets or sets an array of buffers to be sent for an asynchronous operation used by the Socket::SendPacketsAsync method.
Public propertySendPacketsFlagsGets or sets a bitwise combination of TransmitFileOptions values for an asynchronous operation used by the Socket::SendPacketsAsync method.
Public propertySendPacketsSendSizeGets or sets the size, in bytes, of the data block used in the send operation.
Public propertySocketErrorGets or sets the result of the asynchronous socket operation.
Public propertySocketFlagsGets the results of an asynchronous socket operation or sets the behavior of an asynchronous operation.
Public propertyUserTokenGets or sets a user or application object associated with this asynchronous socket operation.
Top

  NameDescription
Public methodDisposeReleases the unmanaged resources used by the SocketAsyncEventArgs instance and optionally disposes of the managed resources.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeFrees resources used by the SocketAsyncEventArgs class. (Overrides Object::Finalize().)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodOnCompletedRepresents a method that is called when an asynchronous operation completes.
Public methodSetBuffer(Int32, Int32)Sets the data buffer to use with an asynchronous socket method.
Public methodSetBuffer(array<Byte>, Int32, Int32)Sets the data buffer to use with an asynchronous socket method.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public eventCompletedThe event used to complete an asynchronous operation.
Top

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:

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

  2. 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).

  3. Call the appropriate socket method (xxxAsync) to initiate the asynchronous operation.

  4. If the asynchronous socket method (xxxAsync) returns true, in the callback, query the context properties for completion status.

  5. If the asynchronous socket method (xxxAsync) returns false, the operation completed synchronously. The context properties may be queried for the operation result.

  6. 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.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4, 3.5 SP1, 3.0 SP1, 2.0 SP1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show: