Csharp/C Sharp/Network/IP Address — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 15:31, 26 мая 2010
Содержание
Any IPAddress
using System;
using System.Net;
class AddressSample {
public static void Main() {
IPAddress test4 = IPAddress.Any;
Console.WriteLine("The ANY address is: {0}",test4.ToString());
}
}
Broadcast IPAddress
using System;
using System.Net;
class AddressSample {
public static void Main() {
IPAddress test3 = IPAddress.Broadcast;
Console.WriteLine("Broadcast address: {0}",test3.ToString());
}
}
Get Host By Name, Get Host Name
using System;
using System.Net;
class AddressSample {
public static void Main() {
IPHostEntry ihe = Dns.GetHostByName(Dns.GetHostName());
IPAddress myself = ihe.AddressList[0];
Console.WriteLine("The NONE address is: {0}",myself);
}
}
Get Host Entry
using System;
using System.Net;
class MainClass {
public static void Main(string[] args) {
foreach (string comp in args) {
try {
IPAddress[] addresses = Dns.GetHostEntry(comp).AddressList;
foreach (IPAddress address in addresses) {
Console.WriteLine("{0} = {1} ({2})",
comp, address, address.AddressFamily);
}
} catch (Exception ex) {
Console.WriteLine("{0} = Error ({1})", comp, ex.Message);
}
}
}
}
IPAddress AddressFamily
using System;
using System.Net;
class MainClass {
public static void Main(string[] args) {
foreach (string comp in args) {
try {
IPAddress[] addresses = Dns.GetHostEntry(comp).AddressList;
foreach (IPAddress address in addresses) {
Console.WriteLine("{0} = {1} ({2})",
comp, address, address.AddressFamily);
}
} catch (Exception ex) {
Console.WriteLine("{0} = Error ({1})", comp, ex.Message);
}
}
}
}
IP Address parse, lookup
using System;
using System.Net;
public class AddressSample
{
public static void Main()
{
IPAddress test1 = IPAddress.Parse("192.168.1.1");
IPAddress test2 = IPAddress.Loopback;
IPAddress test3 = IPAddress.Broadcast;
IPAddress test4 = IPAddress.Any;
IPAddress test5 = IPAddress.None;
IPHostEntry ihe = Dns.GetHostByName(Dns.GetHostName());
IPAddress myself = ihe.AddressList[0];
if (IPAddress.IsLoopback(test2))
Console.WriteLine("The Loopback address is: {0}", test2.ToString());
else
Console.WriteLine("Error obtaining the loopback address");
Console.WriteLine("The Local IP address is: {0}\n", myself.ToString());
if (myself == test2)
Console.WriteLine("The loopback address is the same as local address.\n");
else
Console.WriteLine("The loopback address is not the local address.\n");
Console.WriteLine("The test address is: {0}", test1.ToString());
Console.WriteLine("Broadcast address: {0}", test3.ToString());
Console.WriteLine("The ANY address is: {0}", test4.ToString());
Console.WriteLine("The NONE address is: {0}", test5.ToString());
}
}
IPEndPoint sample
using System;
using System.Net;
public class IPEndPointSample
{
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}\n",
ie.Address, ie.Port);
Console.WriteLine("The min port number is: {0}", IPEndPoint.MinPort);
Console.WriteLine("The max port number is: {0}\n", IPEndPoint.MaxPort);
ie.Port = 80;
Console.WriteLine("The changed IPEndPoint value is: {0}", ie.ToString());
SocketAddress sa = ie.Serialize();
Console.WriteLine("The SocketAddress is: {0}", sa.ToString());
}
}
IsLoopback IPAddress
using System;
using System.Net;
class AddressSample {
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");
}
}
Loopback IPAddress
using System;
using System.Net;
class AddressSample {
public static void Main() {
IPAddress test2 = IPAddress.Loopback;
Console.WriteLine("Broadcast address: {0}",test2.ToString());
}
}
None IPAddress
using System;
using System.Net;
class AddressSample {
public static void Main() {
IPAddress test5 = IPAddress.None;
Console.WriteLine("The NONE address is: {0}",test5.ToString());
}
}
Parse an IPAddress
using System;
using System.Net;
class AddressSample {
public static void Main() {
IPAddress test1 = IPAddress.Parse("192.168.1.1");
Console.WriteLine("The test address is: {0}", test1.ToString());
}
}