IPaddress, ipendpoint, and iphostentry are located in the namespace system. NET and provide IP address operations.
IPaddress is an IP address class encapsulated by. net.
Common Methods:
1. IPaddress. parse ()
Public static IPaddress parse (string ipstring). This method is used to convert an ipstring to the IPaddress type.
2. IPaddress. loopback, IPaddress. Broadcast, IPaddress. Any, and IPaddress. None are all domain members of IPaddress. The returned values of these fields are all IPaddress.
Class ipaddresssample {public static void main () {// create five IPaddress objects and assign IPaddress newaddress1 = IPaddress. parse ("192.168.1.1"); // converts 192.168.1.1 to IPaddress newaddress2 = IPaddress. loopback; // The local loopback address IPaddress newaddress3 = IPaddress. broadcast; // broadcast address IPaddress newaddress4 = IPaddress. any; IPaddress newaddress5 = IPaddress. none;/* use system.net. the gethostentry () and gethostname () methods in the DNS class are used to create a local IP address, and An iphostentry object is created. Use the following two statements Code To obtain the local IP Address */iphostentry here = DNS. gethostentry (DNS. gethostname (); IPaddress localaddress = here. addresslist [0]; // determines whether the newaddress2 address is a loopback address if (IPaddress. isloopback (newaddress2) console. writeline ("the loopback address is: {0}", newaddress2.tostring (); else console. writeline ("error obtaining the loopback address"); // print the local IP address console. writeline ("the local IP address is: {0} \ n", localaddress. tostring (); // determines whether the local IP address is a loopback address. If (localaddress = newaddress2) console. writeline ("the loopback address is the same as local address. \ n "); else console. writeline ("the loopback address is not the local address. \ n "); // print other IP address console. writeline ("the test address is: {0}", newaddress1.tostring (); console. writeline ("broadcast address: {0}", newaddress3.tostring (); console. writeline ("the any address is: {0}", newaddress4.tostring (); console. writeline ("The None address is: {0}", newaddress5.tostring (); // use console. readling () enablesProgram Do not exit immediately after the above code is executed. After the user enters the Enter key, exit the console. Readline ();}
Ipendpoint indicates the IP address and port number of the network endpoint.
Instantiate ipendpoint
IPaddress newaddress = IPaddress. parse ("192.168.1.1"); ipendpoint EX = new ipendpoint (newaddress, 8000); // create an ipendpoint instance
Common attributes and Methods
Ex. Address returns the IP address of the ipendpoint instance.
Ex. Port returns the port of the ipendpoint instance.
Class ipendpointsample {public static void main () {IPaddress newaddress = IPaddress. parse ("192.168.1.1"); // create ipendpoint instance ipendpoint EX = new ipendpoint (newaddress, 8000); console. writeline ("The ipendpoint is: {0}", Ex. tostring (); console. writeline ("The addressfamily is: {0}", Ex. addressfamily); console. writeline ("the address is: {0}, and the port is: {1}", Ex. address, Ex. port); console. writeline ("the min port number is: {0}", ipendpoint. minport); console. writeline ("the max port number is: {0}", ipendpoint. maxport); // use the port attribute to separately change the port value of the ipendpoint object ex. port = 80; console. writeline ("the changed ipendpoint vaule is: {0}", Ex. tostring (); // store ipednpoint instance socketaddress SA = ex. serialize (); console. writeline ("The socketaddress is: {0}", SA. tostring (); console. readline ();}}
Iphostentry indicates the entity of an IP address.
Class iphostentryclasssample {public static void main (string [] argv) {iphostentry Results = DNS. gethostentry (IPaddress. parse ("127.0.0.1"); console. writeline ("Host Name: {0}", results. hostname); foreach (string alias in results. aliases) {console. writeline ("Alias: {0}", alias);} foreach (IPaddress address in results. addresslist) {console. writeline ("Address: {0}", address. tostring ());}}}
Download instance:
Ipaddresssample
Ipendpointsample
Iphostentrysample
Class ipaddresssample {public static void main () {// create five IPaddress objects and assign IPaddress newaddress1 = IPaddress. parse ("192.168.1.1"); // converts 192.168.1.1 to IPaddress newaddress2 = IPaddress. loopback; // The local loopback address IPaddress newaddress3 = IPaddress. broadcast; // broadcast address IPaddress newaddress4 = IPaddress. any; IPaddress newaddress5 = IPaddress. none;/* use system.net. the gethostentry () and gethostname () methods in the DNS class are used to create a local IP address, and An iphostentry object is created. Use the following code to obtain the local IP Address */iphostentry here = DNS. gethostentry (DNS. gethostname (); IPaddress localaddress = here. addresslist [0]; // determines whether the newaddress2 address is a loopback address if (IPaddress. isloopback (newaddress2) console. writeline ("the loopback address is: {0}", newaddress2.tostring (); else console. writeline ("error obtaining the loopback address"); // print the local IP address console. writeline ("the local IP address is: {0} \ n", localaddress. tostring (); // determines whether the local IP address is a loopback address. If (localaddress = newaddress2) console. writeline ("the loopback address is the same as local address. \ n "); else console. writeline ("the loopback address is not the local address. \ n "); // print other IP address console. writeline ("the test address is: {0}", newaddress1.tostring (); console. writeline ("broadcast address: {0}", newaddress3.tostring (); console. writeline ("the any address is: {0}", newaddress4.tostring (); console. writeline ("The None address is: {0}", newaddress5.tostring (); // use console. readling () causes the program to not exit immediately after the code is executed. After the user enters the Enter key, the program console is exited. readline ();}
Class ipaddresssample {public static void main () {// create five IPaddress objects and assign IPaddress newaddress1 = IPaddress. parse ("192.168.1.1"); // converts 192.168.1.1 to IPaddress newaddress2 = IPaddress. loopback; // The local loopback address IPaddress newaddress3 = IPaddress. broadcast; // broadcast address IPaddress newaddress4 = IPaddress. any; IPaddress newaddress5 = IPaddress. none;/* use system.net. the gethostentry () and gethostname () methods in the DNS class are used to create a local IP address, and An iphostentry object is created. Use the following code to obtain the local IP Address */iphostentry here = DNS. gethostentry (DNS. gethostname (); IPaddress localaddress = here. addresslist [0]; // determines whether the newaddress2 address is a loopback address if (IPaddress. isloopback (newaddress2) console. writeline ("the loopback address is: {0}", newaddress2.tostring (); else console. writeline ("error obtaining the loopback address"); // print the local IP address console. writeline ("the local IP address is: {0} \ n", localaddress. tostring (); // determines whether the local IP address is a loopback address. If (localaddress = newaddress2) console. writeline ("the loopback address is the same as local address. \ n "); else console. writeline ("the loopback address is not the local address. \ n "); // print other IP address console. writeline ("the test address is: {0}", newaddress1.tostring (); console. writeline ("broadcast address: {0}", newaddress3.tostring (); console. writeline ("the any address is: {0}", newaddress4.tostring (); console. writeline ("The None address is: {0}", newaddress5.tostring (); // use console. readling () causes the program to not exit immediately after the code is executed. After the user enters the Enter key, the program console is exited. readline ();}