IPEndpoint Class

Represents a network endpoint as an IP address and a port number.

Namespace

System.Net

Assembly

System.Net.Primitives.dll

Syntax

public sealed class IPEndpoint : EndPoint

Remarks

The IPEndpoint class represents a network endpoint, which consists of an IP address and a port number. It is a fundamental class for network programming in .NET, used in various networking classes such as Socket, UdpClient, and TcpClient.

An IPEndpoint object can represent either an IPv4 or an IPv6 endpoint. The AddressFamily property indicates which address family is being used.

It is immutable after creation, meaning its IP address and port number cannot be changed once an IPEndpoint object is initialized.

Constructors

IPEndpoint(long, int)

Initializes a new instance of the IPEndpoint class with the specified IP address and port number.

public IPEndpoint(long newAddress, int newPort)

Parameters:

IPEndpoint(IPAddress, int)

Initializes a new instance of the IPEndpoint class with the specified IP address and port number.

public IPEndpoint(IPAddress newAddress, int newPort)

Parameters:

Properties

Address

Gets the IP address of the endpoint.

public IPAddress Address { get; }

AddressFamily

Gets the address family of the endpoint.

public override AddressFamily AddressFamily { get; }

Port

Gets the port number of the endpoint.

public int Port { get; }

Methods

Equals(object)

Determines whether two IPEndpoint instances are equal.

public override bool Equals(object comparand)

Parameters:

Returns: true if the specified object is an IPEndpoint instance and is equal to the current instance; otherwise, false.

GetHashCode()

Returns the hash code for the current IPEndpoint instance.

public override int GetHashCode()

Returns: A 32-bit signed integer hash code.

Parse(string)

Parses a string representation of an IPEndpoint and returns an IPEndpoint instance.

public static IPEndpoint Parse(string s)

Parameters:

Returns: An IPEndpoint instance created from the specified string.

Throws: ArgumentNullException: If s is null. ArgumentException: If s is not in a valid format.

ToString()

Returns the string representation of the IPEndpoint.

public override string ToString()

Returns: A string that represents the IPEndpoint. The format is "address:port".

Example

The following C# code example demonstrates how to create and use an IPEndpoint.

using System;
using System.Net;

public class IPEndpointExample
{
    public static void Main(string[] args)
    {
        // Create an IPv4 endpoint
        IPAddress ipv4Address = IPAddress.Parse("192.168.1.100");
        int port = 8080;
        IPEndpoint endpointV4 = new IPEndpoint(ipv4Address, port);

        Console.WriteLine($"IPv4 Endpoint: {endpointV4.ToString()}");
        Console.WriteLine($"Address: {endpointV4.Address}");
        Console.WriteLine($"Port: {endpointV4.Port}");
        Console.WriteLine($"Address Family: {endpointV4.AddressFamily}");

        Console.WriteLine("---");

        // Create an IPv6 endpoint
        IPAddress ipv6Address = IPAddress.Parse("2001:db8::1");
        int ipv6Port = 80;
        IPEndpoint endpointV6 = new IPEndpoint(ipv6Address, ipv6Port);

        Console.WriteLine($"IPv6 Endpoint: {endpointV6.ToString()}");
        Console.WriteLine($"Address: {endpointV6.Address}");
        Console.WriteLine($"Port: {endpointV6.Port}");
        Console.WriteLine($"Address Family: {endpointV6.AddressFamily}");

        Console.WriteLine("---");

        // Parse an endpoint from a string
        string endpointString = "10.0.0.5:9000";
        try
        {
            IPEndpoint parsedEndpoint = IPEndpoint.Parse(endpointString);
            Console.WriteLine($"Parsed Endpoint: {parsedEndpoint}");
        }
        catch (ArgumentException ex)
        {
            Console.WriteLine($"Error parsing endpoint: {ex.Message}");
        }
    }
}