SocketAddress Class

System.Net.Sockets.SocketAddress

Summary

Represents an address for a socket. This class cannot be inherited.

Syntax

public sealed class SocketAddress

Remarks

The SocketAddress class represents the address of a socket. It is a variable-sized buffer that contains socket address information specific to the underlying operating system and address family.

Instances of the SocketAddress class are opaque to your application; you should not attempt to interpret or manipulate the contents of the buffer directly. Instead, use the methods and properties of the SocketAddress class to access and modify socket address information.

For example, to get the size of the address, use the Size property. To get the address family, use the Family property.

The SocketAddress class is used in conjunction with the Socket class to establish connections and send and receive data.

Constructors

  • SocketAddress(AddressFamily addressFamily, int size) Initializes a new instance of the SocketAddress class with the specified address family and size.

Properties

  • Family: AddressFamily Gets the address family for this SocketAddress.
  • Size: int Gets the size of the socket address.

Methods

  • GetValue(int offset): byte Gets the byte at the specified offset within the socket address.
  • SetValue(int offset, byte value) Sets the byte at the specified offset within the socket address.
  • GetValue(int offset): short Gets the 16-bit integer at the specified offset within the socket address.
  • SetValue(int offset, short value) Sets the 16-bit integer at the specified offset within the socket address.
  • GetValue(int offset): int Gets the 32-bit integer at the specified offset within the socket address.
  • SetValue(int offset, int value) Sets the 32-bit integer at the specified offset within the socket address.

Example

The following code example demonstrates how to create and populate a SocketAddress for an IPv4 endpoint.

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

public class Example
{
    public static void Main()
    {
        // Create a SocketAddress for an IPv4 address and port.
        // The size should accommodate the IP address and port structure.
        int port = 13000;
        int ipv4AddressSize = 16; // Common size for IPv4 sockaddr_in
        var socketAddress = new SocketAddress(AddressFamily.InterNetwork, ipv4AddressSize);

        // Set the port (offset 2 is common for port in sockaddr_in, network byte order).
        short networkPort = (short)IPAddress.HostToNetworkOrder(port);
        socketAddress.SetValue(2, networkPort);

        // Set the IP address (offset 4 is common for IP address in sockaddr_in, network byte order).
        var ipAddress = IPAddress.Parse("127.0.0.1");
        byte[] ipBytes = ipAddress.GetAddressBytes();
        if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
        {
            // For IPv4, the address is 4 bytes.
            // We need to place it correctly in the byte array for the SocketAddress.
            // The exact offset depends on the OS, but often starts after family and port.
            // For simplicity here, we'll assume a common structure.
            // A more robust solution would involve system-specific structures or managed types.

            // Example of setting the 4 bytes of the IPv4 address
            // This is a simplified representation. Real implementation details can be complex.
            // The structure of SocketAddress is system-dependent.
            // For a typical sockaddr_in, the address starts after the family (2 bytes) and port (2 bytes).
            var addressBytes = ipAddress.GetAddressBytes();
            socketAddress.SetValue(4, addressBytes[3]);
            socketAddress.SetValue(5, addressBytes[2]);
            socketAddress.SetValue(6, addressBytes[1]);
            socketAddress.SetValue(7, addressBytes[0]);
        }

        // You would then use this socketAddress to bind a Socket or connect.
        Console.WriteLine($"Created SocketAddress for {ipAddress}:{port}");
        Console.WriteLine($"Family: {socketAddress.Family}");
        Console.WriteLine($"Size: {socketAddress.Size}");
    }
}