.NET Documentation

Endpoints

This page provides an overview of how endpoints are represented and used within the System.Net.Sockets namespace in .NET.

What are Endpoints?

An endpoint represents a specific network address, including an IP address and a port number. It defines a communication point for network services. In .NET, the primary class for representing network endpoints is System.Net.EndPoint, with concrete implementations like System.Net.IPEndPoint for IP-based protocols (TCP/UDP) and System.Net.UnixDomainSocketEndPoint for Unix domain sockets.

System.Net.EndPoint Class

The abstract base class EndPoint serves as the foundation for all endpoint types. It defines common properties and methods, such as:

System.Net.IPEndPoint Class

IPEndPoint is the most commonly used endpoint class for IP networking. It combines an IPAddress with a port number. It inherits from EndPoint and provides specific members for IP-based communication.

Key Properties and Methods:

Example: Creating and Using an IPEndPoint


using System;
using System.Net;

public class EndPointExample
{
    public static void Main(string[] args)
    {
        // Create an IPEndPoint for an IPv4 address and port
        IPAddress ipAddress = IPAddress.Parse("192.168.1.100");
        int port = 8080;
        IPEndPoint endPoint = new IPEndPoint(ipAddress, port);

        Console.WriteLine($"Endpoint created: {endPoint.ToString()}");
        Console.WriteLine($"Address Family: {endPoint.AddressFamily}");
        Console.WriteLine($"IP Address: {endPoint.Address}");
        Console.WriteLine($"Port: {endPoint.Port}");

        // Example with IPv6
        IPAddress ipv6Address = IPAddress.Parse("2001:db8::1");
        int ipv6Port = 12345;
        IPEndPoint ipv6EndPoint = new IPEndPoint(ipv6Address, ipv6Port);
        Console.WriteLine($"IPv6 Endpoint: {ipv6EndPoint.ToString()}");

        // Creating from a string
        try
        {
            EndPoint anotherEndPoint = EndPoint.Parse("10.0.0.1:9000");
            Console.WriteLine($"Parsed endpoint: {anotherEndPoint.ToString()}");
        }
        catch (FormatException e)
        {
            Console.WriteLine($"Error parsing endpoint: {e.Message}");
        }
    }
}
            

System.Net.UnixDomainSocketEndPoint Class

For scenarios involving Unix-domain sockets (primarily on Linux and macOS), UnixDomainSocketEndPoint represents an endpoint using a file system path.

This class is crucial for inter-process communication on Unix-like systems using sockets.

Serialization and Deserialization

The Serialize() and Create() methods on the EndPoint class are fundamental for converting between the abstract EndPoint representation and the lower-level, protocol-specific SocketAddress structure. This is often handled implicitly by socket classes but can be useful for advanced scenarios.

Note on Port Numbers

Port numbers are used to differentiate services running on the same IP address. Ports 0-1023 are generally reserved for well-known services (e.g., HTTP on port 80, HTTPS on port 443). Applications typically use ports above 1023 for custom services.

Security Considerations

When binding to an endpoint or connecting to a remote endpoint, ensure you are using the correct IP address and port to avoid unintended connections or security vulnerabilities. Binding to IPAddress.Any allows listening on all available network interfaces, which can be convenient but requires careful consideration of which interfaces should be exposed.