How to get the IP address of the machine in C #

zhaozj2021-02-16  86

How to use the DNS class and get the skill of the IP address of the machine

Introduction

This article is not a technical overview or a large discussion, and more like how to get the IP address or host name. In Win32 API programming, you can use the Network API, similar to the .NET platform. The only difference is that you have to find and understand what namespace (Namespace) and classes that need to complete this task. The Network API is stored in the System.Net namespace in the .NET platform. The DNS class in the System.NET namespace can be used to get the machine name and IP address. DNS class provides a simple domain name interpretation function. The DNS class provides support for processing Internet Domain Name (DNS) information. These returned information includes multiple IP addresses and host alias. The list of returned is a collection or an array of iPaddress objects. The following code shows how to get an IP address through a given host name.

DNSUtility Code namespace NKUtilities {using System; using System.Net; public class DNSUtility {public static int Main (string [] args) {String strHostName = new String ( ""); if (args.Length == 0) {// First get the host name of the local machine strHostName = DNS.GETHOSTNAME (); Console.Writeline ("local machine's host name);} else {strhostname = args [0];} // then get the IP address by the host name List iphostentry ipentry = dns.gethostbyname (strHostName); ipaddress [] addr = ipentry.addresslist; for (int i = 0; i

Tip Confirm that in your code already contains the System.Net namespace, otherwise the compiler will not know how to find the DNS class. Similarly, when you use VisualStudio.net to create a project, confirm that your system already contains System.Net.dll. For more detailed information on the DNS class and System.net namespace, please refer to the .NET SDK online documentation.

转载请注明原文地址:https://www.9cbs.com/read-12956.html

New Post(0)