SocketInformation Class

public sealed class SocketInformation

Remarks

Encapsulates all information required to establish a Socket connection using the AcceptSocketAsync method. This class provides a snapshot of the socket's state when it is created.

Example

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

public class SocketInfoExample
{
    public static async Task RunServerAsync(int port)
    {
        using Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        listener.Bind(new IPEndPoint(IPAddress.Any, port));
        listener.Listen(100);

        Console.WriteLine($"Server listening on port {port}");

        while (true)
        {
            var acceptResult = await listener.AcceptSocketAsync();
            if (acceptResult == null) continue;

            SocketInformation socketInfo = acceptResult.DuplicateAndClose(Int32.MaxValue);

            await HandleClientAsync(socketInfo);
        }
    }

    private static async Task HandleClientAsync(SocketInformation info)
    {
        using Socket clientSocket = info.AcceptSocket;
        Console.WriteLine($"Accepted connection from {clientSocket.RemoteEndPoint} using SocketInformation.");

        byte[] buffer = new byte[1024];
        int received = await clientSocket.ReceiveAsync(buffer, SocketFlags.None);
        string message = Encoding.UTF8.GetString(buffer, 0, received);
        Console.WriteLine($"Received: {message}");

        string response = "Hello from the .NET server!";
        byte[] responseBytes = Encoding.UTF8.GetBytes(response);
        await clientSocket.SendAsync(responseBytes, SocketFlags.None);

        clientSocket.Close();
        Console.WriteLine("Connection closed.");
    }

    public static async Task RunClientAsync(string host, int port)
    {
        using Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        await client.ConnectAsync(host, port);

        string messageToSend = "Hello from the .NET client!";
        byte[] messageBytes = Encoding.UTF8.GetBytes(messageToSend);
        await client.SendAsync(messageBytes, SocketFlags.None);

        byte[] buffer = new byte[1024];
        int received = await client.ReceiveAsync(buffer, SocketFlags.None);
        string response = Encoding.UTF8.GetString(buffer, 0, received);
        Console.WriteLine($"Server response: {response}");

        client.Close();
    }

    public static async Task Main(string[] args)
    {
        // Start the server in a background task
        _ = RunServerAsync(8080);
        // Give the server a moment to start
        await Task.Delay(1000);
        // Run the client
        await RunClientAsync("127.0.0.1", 8080);
    }
}

Members

Properties

Socket AcceptSocket

Gets the socket that is created by the call to AcceptSocketAsync.

Property Value

The Socket that is created by the call to AcceptSocketAsync.

EndPoint RemoteEndPoint

Gets the remote host information for the socket.

Property Value

An EndPoint instance that contains the remote host information.

EndPoint LocalEndPoint

Gets the local host information for the socket.

Property Value

An EndPoint instance that contains the local host information.

Methods

This class does not expose any public methods beyond those inherited from System.Object.