Csharp/CSharp Tutorial/Network/IP Address
Содержание
- 1 Create IPEndPoint from IPAddress
- 2 Create(parse) IP address from string
- 3 Get Address Family, address and port from an IPEndPoint
- 4 Get Alias from IPHostEntry
- 5 IPAddress.Any
- 6 IPAddress.Broadcast
- 7 IPAddress.HostToNetworkOrder
- 8 IPAddress.Loopback
- 9 IPAddress.None
- 10 IPEndPoint: MinPort and MaxPort
- 11 Is Loopback IP address
- 12 Serialize a SocketAddress from a IPEndPoint
- 13 Set Port for an IPEndPoint
Create IPEndPoint from IPAddress
<source lang="csharp">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); }
}</source>
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
<source lang="csharp">using System; using System.Net; class MainClass {
public static void Main() { IPAddress test1 = IPAddress.Parse("192.168.1.1"); Console.WriteLine(test1); }
}</source>
192.168.1.1
Get Address Family, address and port from an IPEndPoint
<source lang="csharp">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); }
}</source>
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
<source lang="csharp">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); } }
}</source>
IPAddress.Any
<source lang="csharp">using System; using System.Net; class MainClass {
public static void Main() { IPAddress test = IPAddress.Any; Console.WriteLine(test); }
}</source>
0.0.0.0
IPAddress.Broadcast
<source lang="csharp">using System; using System.Net; class MainClass {
public static void Main() { IPAddress test = IPAddress.Broadcast; Console.WriteLine(test); }
}</source>
255.255.255.255
IPAddress.HostToNetworkOrder
<source lang="csharp">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); }
}</source>
IPAddress.Loopback
<source lang="csharp">using System; using System.Net; class MainClass {
public static void Main() { IPAddress test = IPAddress.Loopback; Console.WriteLine(test); }
}</source>
127.0.0.1
IPAddress.None
<source lang="csharp">using System; using System.Net; class MainClass {
public static void Main() { IPAddress test = IPAddress.None; Console.WriteLine(test); }
}</source>
255.255.255.255
IPEndPoint: MinPort and MaxPort
<source lang="csharp">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); }
}</source>
The min port number is: 0 The max port number is: 65535
Is Loopback IP address
<source lang="csharp">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"); }
}</source>
The Loopback address is: 127.0.0.1
Serialize a SocketAddress from a IPEndPoint
<source lang="csharp">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()); }
}</source>
The SocketAddress is: InterNetwork:16:{31,64,192,168,1,1,0,0,0,0,0,0,0,0}
Set Port for an IPEndPoint
<source lang="csharp">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()); }
}</source>
The changed IPEndPoint value is: 192.168.1.1:80