SocketAddress Class

Namespace: System.Net.Sockets Assembly: System.Net.Primitives.dll

Represents a socket address. This class is an abstract base class.

Syntax

public abstract class SocketAddress

Remarks

The SocketAddress class provides a way to represent the socket address structure used by the underlying network protocol. It serves as a base class for protocol-specific implementations of socket addresses, such as those for Internet Protocol version 4 (IPv4) and Internet Protocol version 6 (IPv6).

You typically interact with the concrete derived classes that represent specific address families, rather than directly with SocketAddress itself. For example, when working with IPv4 addresses, you will commonly use an IPEndPoint, which internally manages an address derived from SocketAddress.

This class is fundamental to the System.Net.Sockets namespace, enabling the Socket class to communicate with other network devices by specifying their addresses.

Inheritance Hierarchy

Derived Classes

SocketAddress is the abstract base class. Concrete implementations include:

Note: In many common scenarios, you'll work with higher-level classes like IPEndPoint, which abstract away the direct manipulation of SocketAddress objects.

Constructors

The SocketAddress class does not have public constructors because it is an abstract class. Derived classes will provide their own constructors.

Properties

Name Description
Family Gets the address family of the socket address.
Size Gets the size of the socket address in bytes.

Methods

Name Description
GetPrt() Gets the port number from the socket address.
GetAddressBytes() Gets the address bytes from the socket address.
SetPort(int port) Sets the port number in the socket address.
SetAddress(byte[] address) Sets the address in the socket address.

Example

The following example demonstrates how to create and use an IPEndPoint, which internally uses a SocketAddress representation.

using System;
using System.Net;
using System.Net.Sockets;

public class SocketAddressExample
{
    public static void Main(string[] args)
    {
        // Create an IPEndPoint for an IPv4 address and port.
        // IPEndPoint internally manages a SocketAddress.
        IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.100"), 12345);

        Console.WriteLine($"IP End Point: {ipEndPoint}");
        Console.WriteLine($"Address Family: {ipEndPoint.AddressFamily}");
        Console.WriteLine($"Port: {ipEndPoint.Port}");

        // You can also get the underlying SocketAddress if needed,
        // though this is less common in typical application code.
        // The actual type of SocketAddress returned depends on the AddressFamily.
        // For IPAddress.IPv4, it's usually an internal struct.
        // Here we demonstrate accessing properties through reflection for illustration.
        try
        {
            // In a real scenario, you'd likely use a Socket object to get
            // the SocketAddress in a more direct way, or rely on IPEndPoint.
            // This is for illustrative purposes to show the concept.
            Console.WriteLine("\nAccessing underlying SocketAddress concepts:");

            // The following is a conceptual representation and might not work directly
            // depending on internal .NET Core implementation details for SocketAddress.
            // However, the principles of Family and Size apply.

            // Get the internal representation (conceptual)
            // This part is tricky as SocketAddress is abstract and its concrete types are internal.
            // We can infer properties based on IPEndPoint.
            Console.WriteLine($"SocketAddress Size (conceptual): {ipEndPoint.Serialize().Size}");
            Console.WriteLine($"SocketAddress Family (conceptual): {ipEndPoint.AddressFamily}");

            // Note: Directly manipulating SocketAddress is advanced.
            // Use IPEndPoint for ease of use.
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}