Provides classes that support IP addressing, including representations of IP addresses, IP address prefixes, and network endpoints. This namespace is fundamental for network programming in .NET, enabling operations like creating, parsing, and manipulating IPv4 and IPv6 addresses.
Classes
- IPAddress
- IPAddressPolicyFilter
- IPAddressPolicyFilterCollection
- IPAddressRestrictionEntry
- IPAddressRestrictionCollection
- IPAddressResolver
- IpTools
- IPAddressInfo
- IPEndPoint
Structs
Enums
Overview
The System.Net.IP
namespace provides core classes for working with IP (Internet Protocol) addresses and network endpoints in .NET applications. These classes are essential for any network communication, allowing developers to represent, manipulate, and validate IP addresses (both IPv4 and IPv6), define network ranges, and construct network endpoints.
Key classes in this namespace include:
- IPAddress: Represents an Internet Protocol (IP) address. You can use this class to create, parse, and manipulate IP addresses. It supports both IPv4 and IPv6 formats.
- IPEndPoint: Represents a network endpoint as an IP address and a port number. This is crucial for establishing connections or sending datagrams to specific network services.
- IPNetwork and IPNetworkPrefix: Provide functionality to define and work with IP address ranges and network masks, often used for routing or filtering.
- IPVersion: An enumeration that specifies the IP protocol version, typically IPv4 or IPv6.
Example Usage
Here's a simple example of how to create and display an IP address and an endpoint:
using System;
using System.Net;
public class IpDemo
{
public static void Main(string[] args)
{
// Create an IPv4 address
IPAddress ipv4Address = IPAddress.Parse("192.168.1.100");
Console.WriteLine($"IPv4 Address: {ipv4Address}");
// Create an IPv6 address
IPAddress ipv6Address = IPAddress.Parse("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
Console.WriteLine($"IPv6 Address: {ipv6Address}");
// Create an IPEndPoint
IPEndPoint endpoint = new IPEndPoint(ipv4Address, 8080);
Console.WriteLine($"IP Endpoint: {endpoint}");
// Check if the address is IPv4
Console.WriteLine($"Is IPv4: {ipv4Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork}");
// Check if the address is IPv6
Console.WriteLine($"Is IPv6: {ipv6Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6}");
}
}
This namespace is foundational for building robust networking applications in .NET, from simple web servers to complex distributed systems.