SocketAsyncStatistic Class
Namespace: System.Net.Sockets
Assembly: System.Net.Sockets.dll
Overview
Represents statistics for asynchronous socket operations. This class is used to retrieve information about the performance and usage of asynchronous socket methods within the .NET Framework.
It provides details on the number of operations, the latency, and the number of socket errors encountered during asynchronous communication.
Constructors
-
SocketAsyncStatistic()Initializes a new instance of theSocketAsyncStatisticclass.
Properties
-
long Absolute.ConnectCountGets the total number of successful socket connect operations. -
long Absolute.ConnectErrorCountGets the total number of failed socket connect operations. -
long Absolute.DisconnectCountGets the total number of successful socket disconnect operations. -
long Absolute.DisconnectErrorCountGets the total number of failed socket disconnect operations. -
long Absolute.ReceiveCountGets the total number of successful socket receive operations. -
long Absolute.ReceiveErrorCountGets the total number of failed socket receive operations. -
long Absolute.SendCountGets the total number of successful socket send operations. -
long Absolute.SendErrorCountGets the total number of failed socket send operations. -
long Absolute.Overall.ConnectDurationGets the total duration of all successful socket connect operations. -
long Absolute.Overall.DisconnectDurationGets the total duration of all successful socket disconnect operations. -
long Absolute.Overall.ReceiveDurationGets the total duration of all successful socket receive operations. -
long Absolute.Overall.SendDurationGets the total duration of all successful socket send operations. -
long Relative.ConnectCountGets the count of successful socket connect operations since the last reset. -
long Relative.ConnectErrorCountGets the count of failed socket connect operations since the last reset. -
long Relative.DisconnectCountGets the count of successful socket disconnect operations since the last reset. -
long Relative.DisconnectErrorCountGets the count of failed socket disconnect operations since the last reset. -
long Relative.ReceiveCountGets the count of successful socket receive operations since the last reset. -
long Relative.ReceiveErrorCountGets the count of failed socket receive operations since the last reset. -
long Relative.SendCountGets the count of successful socket send operations since the last reset. -
long Relative.SendErrorCountGets the count of failed socket send operations since the last reset. -
long Relative.Overall.ConnectDurationGets the total duration of successful socket connect operations since the last reset. -
long Relative.Overall.DisconnectDurationGets the total duration of successful socket disconnect operations since the last reset. -
long Relative.Overall.ReceiveDurationGets the total duration of successful socket receive operations since the last reset. -
long Relative.Overall.SendDurationGets the total duration of successful socket send operations since the last reset.
Methods
-
void Reset()Resets the relative statistics to zero. This is useful for periodic performance monitoring. -
SocketAsyncStatistic Clone()Creates a deep copy of the currentSocketAsyncStatisticobject.
Remarks
The SocketAsyncStatistic class allows developers to gain insights into the efficiency of their network applications. By monitoring these statistics, you can identify potential bottlenecks, diagnose performance issues, and optimize your socket operations. The Absolute properties provide cumulative counts and durations since the application started, while the Relative properties reflect values since the last call to Reset().
Example
using System.Net.Sockets;
using System.Threading.Tasks;
public class SocketStatsExample
{
public static async void Run()
{
// Assume serverSocket is an initialized Socket object
var serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Bind and listen for connections...
// Get the statistics object
var stats = serverSocket.SocketAsyncOperations;
// Perform some asynchronous operations
// For example, accept a connection:
var acceptArgs = new SocketAsyncEventArgs();
// ... configure acceptArgs ...
bool completedSynchronously = await serverSocket.AcceptAsync(acceptArgs);
// Access the statistics
Console.WriteLine("Total Connects: {0}", stats.Absolute.ConnectCount);
Console.WriteLine("Total Receives: {0}", stats.Absolute.ReceiveCount);
Console.WriteLine("Total Connect Errors: {0}", stats.Absolute.ConnectErrorCount);
// Reset relative stats for subsequent measurement
stats.Reset();
// Perform more operations...
// Access relative statistics
Console.WriteLine("Relative Receives: {0}", stats.Relative.ReceiveCount);
}
}