IPAddress

The IPAddress class in the System.Net namespace represents an address in either protocol.

It has a constructor accepting a byte array, and a static Parse method accepting a correctly formatted string:


using System;
using System.Net;

using System.Linq;
using System.Text;

class Program
{
    static void Main()
    {
        IPAddress a1 = new IPAddress(new byte[] { 101, 102, 103, 104 });
        IPAddress a2 = IPAddress.Parse("101.102.103.104"); 
        Console.WriteLine(a1.Equals(a2)); // True
        Console.WriteLine(a1.AddressFamily);  // InterNetwork
        IPAddress a3 = IPAddress.Parse("[EEA0:FFFF:EEEA:EEA3:4FF2:54fA:41BC:8D31]"); 
        Console.WriteLine(a3.AddressFamily);  // InterNetworkV6
    }
}

The output:


True
InterNetwork
InterNetworkV6

An IP address and port combination is represented in the .NET Framework by the IPEndPoint class:


using System;
using System.Net;

using System.Linq;
using System.Text;

class Program
{
    static void Main()
    {
        IPAddress a = IPAddress.Parse("101.102.103.104");
        IPEndPoint ep = new IPEndPoint(a, 222); // Port 222
        Console.WriteLine(ep.ToString());
    }
}

The output:


101.102.103.104:222
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.