Represents a network endpoint as an IP address and a port number.
System.Net
System.Net.Primitives.dll
public sealed class IPEndpoint : EndPoint
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.
Initializes a new instance of the IPEndpoint
class with the specified IP address and port number.
public IPEndpoint(long newAddress, int newPort)
Parameters:
long
representing the IP address. Note: This overload is generally used for IPv4 addresses and requires careful handling of byte order for non-standard inputs. It's recommended to use IPAddress
for clarity.int
representing the port number.Initializes a new instance of the IPEndpoint
class with the specified IP address and port number.
public IPEndpoint(IPAddress newAddress, int newPort)
Parameters:
IPAddress
object representing the IP address.int
representing the port number.Gets the IP address of the endpoint.
public IPAddress Address { get; }
Gets the address family of the endpoint.
public override AddressFamily AddressFamily { get; }
Gets the port number of the endpoint.
public int Port { get; }
Determines whether two IPEndpoint
instances are equal.
public override bool Equals(object comparand)
Parameters:
IPEndpoint
instance.Returns: true
if the specified object is an IPEndpoint
instance and is equal to the current instance; otherwise, false
.
Returns the hash code for the current IPEndpoint
instance.
public override int GetHashCode()
Returns: A 32-bit signed integer hash code.
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.
Returns the string representation of the IPEndpoint
.
public override string ToString()
Returns: A string that represents the IPEndpoint
. The format is "address:port".
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}");
}
}
}