IPEndPoint Class
The System.Net.IPEndPoint
class represents a network endpoint as an IP address and a port number.
Namespace
System.Net
Assembly
System.Net.Primitives.dll
Syntax
public class IPEndPoint : EndPoint
{
public IPEndPoint(IPAddress address, int port);
public IPAddress Address { get; set; }
public int Port { get; set; }
public override AddressFamily AddressFamily { get; }
public override SocketAddress Serialize();
public override EndPoint Create(SocketAddress socketAddress);
public static IPEndPoint Parse(string s);
public static bool TryParse(string s, out IPEndPoint result);
}
Properties
Property | Type | Description |
---|---|---|
Address | IPAddress | Gets or sets the IP address of the endpoint. |
Port | int | Gets or sets the port number of the endpoint. |
AddressFamily | AddressFamily | Gets the address family of the endpoint. |
Methods
Serialize()
– Returns the endpoint in aSocketAddress
format.Create(SocketAddress)
– Creates a newIPEndPoint
from aSocketAddress
.Parse(string)
– Converts a string representation of an endpoint to anIPEndPoint
.TryParse(string, out IPEndPoint)
– Tries to parse a string into anIPEndPoint
without throwing exceptions.
Examples
// Create an IPEndPoint from an IP address and a port.
IPAddress ip = IPAddress.Parse("192.168.1.100");
int port = 8080;
IPEndPoint endpoint = new IPEndPoint(ip, port);
Console.WriteLine($"Endpoint: {endpoint}");
// Parse from a string.
IPEndPoint parsed = IPEndPoint.Parse("10.0.0.5:5000");
Console.WriteLine($"Parsed: {parsed.Address} on port {parsed.Port}");
// TryParse usage.
if (IPEndPoint.TryParse("invalid:port", out IPEndPoint result))
{
Console.WriteLine("Parsed successfully");
}
else
{
Console.WriteLine("Failed to parse");
}