IPEndPoint Class

Summary

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

The IPEndPoint class is fundamental for network programming in .NET. It encapsulates the network address of a specific network service, which consists of an IP address and a port number. This class is commonly used with socket-based communication to establish connections or send data to remote hosts.

Note: This class provides an abstraction that can work with both IPv4 and IPv6 addresses.

Syntax

public class IPEndPoint : EndPoint

Constructors

Signature Description
IPEndPoint(long address, int port) Initializes a new instance of the IPEndPoint class with the specified IP address and port number. The address parameter is a long that represents the IP address.
IPEndPoint(IPAddress address, int port) Initializes a new instance of the IPEndPoint class with the specified IP address and port number.

Properties

Name Type Description
Address IPAddress Gets or sets the IP address of the endpoint.
Port int Gets or sets the port number of the endpoint. Valid port numbers range from 0 to 65535.

Methods

Name Description
Create(SocketAddress socketAddress) Creates an IPEndPoint from a SocketAddress instance.
Equals(object obj) Determines whether the specified object is equal to the current object.
GetHashCode() Serves as the default hash function.
GetType() Gets the Type of the current instance.
Parse(string s) Converts a string representation of an IP address and port number to an IPEndPoint instance.
ToString() Returns a string representation of the IPEndPoint.
Translate(AddressFamily addressFamily) Translates an IPEndPoint to a specified AddressFamily.

Remarks

An endpoint represents one end of a two-way communication link between two programs running on the network. The IPEndPoint class represents an endpoint using an IP address (either IPv4 or IPv6) and a port number. For example, a web server might listen on port 80 for incoming requests at the IP address 192.168.1.100. This would be represented by an IPEndPoint object.

The port number identifies a specific process or service on the host. Well-known ports are registered with the IANA (Internet Assigned Numbers Authority) and are typically used by common services (e.g., HTTP uses port 80, FTP uses port 21). Dynamic or private ports are available for general application use.

When working with sockets, an IPEndPoint is used to specify the local or remote address for communication.

Tip: Use the IPAddress.Parse() method to create an IPAddress object from a string before passing it to the IPEndPoint constructor.

Example Usage:

using System;
using System.Net;

public class Example
{
    public static void Main(string[] args)
    {
        // Creating an IPEndPoint for a TCP connection to a web server
        IPAddress ipAddress = IPAddress.Parse("192.168.1.100");
        int port = 80;
        IPEndPoint remoteEndPoint = new IPEndPoint(ipAddress, port);

        Console.WriteLine($"Remote Endpoint: {remoteEndPoint.ToString()}");
        Console.WriteLine($"IP Address: {remoteEndPoint.Address}");
        Console.WriteLine($"Port: {remoteEndPoint.Port}");

        // Creating an IPEndPoint for a UDP listener on the local machine
        IPAddress localIpAddress = IPAddress.Any; // Listens on all network interfaces
        int listenerPort = 11000;
        IPEndPoint localEndPoint = new IPEndPoint(localIpAddress, listenerPort);

        Console.WriteLine($"Local Listener Endpoint: {localEndPoint.ToString()}");
    }
}