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 the SocketAsyncStatistic class.

Properties

  • long Absolute.ConnectCount
    Gets the total number of successful socket connect operations.
  • long Absolute.ConnectErrorCount
    Gets the total number of failed socket connect operations.
  • long Absolute.DisconnectCount
    Gets the total number of successful socket disconnect operations.
  • long Absolute.DisconnectErrorCount
    Gets the total number of failed socket disconnect operations.
  • long Absolute.ReceiveCount
    Gets the total number of successful socket receive operations.
  • long Absolute.ReceiveErrorCount
    Gets the total number of failed socket receive operations.
  • long Absolute.SendCount
    Gets the total number of successful socket send operations.
  • long Absolute.SendErrorCount
    Gets the total number of failed socket send operations.
  • long Absolute.Overall.ConnectDuration
    Gets the total duration of all successful socket connect operations.
  • long Absolute.Overall.DisconnectDuration
    Gets the total duration of all successful socket disconnect operations.
  • long Absolute.Overall.ReceiveDuration
    Gets the total duration of all successful socket receive operations.
  • long Absolute.Overall.SendDuration
    Gets the total duration of all successful socket send operations.
  • long Relative.ConnectCount
    Gets the count of successful socket connect operations since the last reset.
  • long Relative.ConnectErrorCount
    Gets the count of failed socket connect operations since the last reset.
  • long Relative.DisconnectCount
    Gets the count of successful socket disconnect operations since the last reset.
  • long Relative.DisconnectErrorCount
    Gets the count of failed socket disconnect operations since the last reset.
  • long Relative.ReceiveCount
    Gets the count of successful socket receive operations since the last reset.
  • long Relative.ReceiveErrorCount
    Gets the count of failed socket receive operations since the last reset.
  • long Relative.SendCount
    Gets the count of successful socket send operations since the last reset.
  • long Relative.SendErrorCount
    Gets the count of failed socket send operations since the last reset.
  • long Relative.Overall.ConnectDuration
    Gets the total duration of successful socket connect operations since the last reset.
  • long Relative.Overall.DisconnectDuration
    Gets the total duration of successful socket disconnect operations since the last reset.
  • long Relative.Overall.ReceiveDuration
    Gets the total duration of successful socket receive operations since the last reset.
  • long Relative.Overall.SendDuration
    Gets 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 current SocketAsyncStatistic object.

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);
    }
}