Csharp/CSharp Tutorial/Network/IP Address — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:20, 26 мая 2010

Create IPEndPoint from IPAddress

using System;
using System.Net;

class MainClass
{
    public static void Main()
    {
        IPAddress test1 = IPAddress.Parse("192.168.1.1");
      IPEndPoint ie = new IPEndPoint(test1, 8000);
    
      Console.WriteLine("The IPEndPoint is: {0}", ie.ToString());
      Console.WriteLine("The AddressFamily is: {0}", ie.AddressFamily);
      Console.WriteLine("The address is: {0}, and the port is: {1}", ie.Address, ie.Port);
    
    }
}
The IPEndPoint is: 192.168.1.1:8000
The AddressFamily is: InterNetwork
The address is: 192.168.1.1, and the port is: 8000

Create(parse) IP address from string

using System;
using System.Net;
class MainClass
{
    public static void Main()
    {
        IPAddress test1 = IPAddress.Parse("192.168.1.1");
        Console.WriteLine(test1);
    }
}
192.168.1.1

Get Address Family, address and port from an IPEndPoint

using System;
using System.Net;

class MainClass
{
    public static void Main()
    {
        IPAddress test1 = IPAddress.Parse("192.168.1.1");
      IPEndPoint ie = new IPEndPoint(test1, 8000);
    
      Console.WriteLine("The IPEndPoint is: {0}", ie.ToString());
      Console.WriteLine("The AddressFamily is: {0}", ie.AddressFamily);
      Console.WriteLine("The address is: {0}, and the port is: {1}", ie.Address, ie.Port);
    
    }
}
The IPEndPoint is: 192.168.1.1:8000
The AddressFamily is: InterNetwork
The address is: 192.168.1.1, and the port is: 8000

Get Alias from IPHostEntry

using System;
using System.Net;
class MainClass
{
   public static void Main(string[] argv)
   {
      IPAddress test = IPAddress.Parse("64.200.123.1");
      IPHostEntry iphe = Dns.GetHostByAddress(test);
      foreach(string alias in iphe.Aliases)
      {
         Console.WriteLine("Alias: {0}", alias);
      }
   }
}

IPAddress.Any

using System;
using System.Net;
class MainClass
{
    public static void Main()
    {
        IPAddress test = IPAddress.Any;
        Console.WriteLine(test);
    }
}
0.0.0.0

IPAddress.Broadcast

using System;
using System.Net;
class MainClass
{
    public static void Main()
    {
        IPAddress test = IPAddress.Broadcast;
        Console.WriteLine(test);
    }
}
255.255.255.255

IPAddress.HostToNetworkOrder

using System;
using System.Net;
using System.Text;
class MainClass
{
   public static void Main()
   {
      short test1 = 45;
      int test2 = 314159;
      long test3 = -123456789033452;
      byte[] data = new byte[1024];
      string output;
      short test1b = IPAddress.HostToNetworkOrder(test1);
      data = BitConverter.GetBytes(test1b);
      output = BitConverter.ToString(data);
      Console.WriteLine("test1 = {0}, nbo = {1}", test1b, output);
      int test2b = IPAddress.HostToNetworkOrder(test2);
      data = BitConverter.GetBytes(test2b);
      output = BitConverter.ToString(data);
      Console.WriteLine("test2 = {0}, nbo = {1}", test2b, output);
      long test3b = IPAddress.HostToNetworkOrder(test3);
      data = BitConverter.GetBytes(test3b);
      output = BitConverter.ToString(data);
      Console.WriteLine("test3 = {0}, nbo = {1}", test3b, output);
   }
}

IPAddress.Loopback

using System;
using System.Net;
class MainClass
{
    public static void Main()
    {
        IPAddress test = IPAddress.Loopback;
        Console.WriteLine(test);
    }
}
127.0.0.1

IPAddress.None

using System;
using System.Net;
class MainClass
{
    public static void Main()
    {
        IPAddress test = IPAddress.None;
        Console.WriteLine(test);
    }
}
255.255.255.255

IPEndPoint: MinPort and MaxPort

using System;
using System.Net;

class MainClass
{
    public static void Main()
    {
      Console.WriteLine("The min port number is: {0}", IPEndPoint.MinPort);
      Console.WriteLine("The max port number is: {0}\n", IPEndPoint.MaxPort);
    }
}
The min port number is: 0
The max port number is: 65535

Is Loopback IP address

using System;
using System.Net;
class MainClass
{
    public static void Main()
    {
      IPAddress test2 = IPAddress.Loopback;
      if (IPAddress.IsLoopback(test2))
          Console.WriteLine("The Loopback address is: {0}", test2.ToString());
      else
          Console.WriteLine("Error obtaining the loopback address");
    }
}
The Loopback address is: 127.0.0.1

Serialize a SocketAddress from a IPEndPoint

using System;
using System.Net;

class MainClass
{
    public static void Main()
    {
        IPAddress test1 = IPAddress.Parse("192.168.1.1");
      IPEndPoint ie = new IPEndPoint(test1, 8000);
    
      SocketAddress sa = ie.Serialize();
      Console.WriteLine("The SocketAddress is: {0}", sa.ToString());
    
    }
}
The SocketAddress is: InterNetwork:16:{31,64,192,168,1,1,0,0,0,0,0,0,0,0}

Set Port for an IPEndPoint

using System;
using System.Net;

class MainClass
{
    public static void Main()
    {
        IPAddress test1 = IPAddress.Parse("192.168.1.1");
      IPEndPoint ie = new IPEndPoint(test1, 8000);
    
      ie.Port = 80;
      Console.WriteLine("The changed IPEndPoint value is: {0}", ie.ToString());
    
    }
}
The changed IPEndPoint value is: 192.168.1.1:80