|
VisualC # is the next-generation programming language launched by Microsoft. It is Microsoft. an important part of the Net Framework. During the release of VisualC #, Microsoft also launched a software development kit --. net FrameWorkSDK. This software development kit encapsulates many classes and objects. Visual C # implements many powerful functions by calling these classes and objects. The. Net FrameWork SDK provides two namespaces for network programming: System. Net and System. Net. Socket. This article uses the classes and objects encapsulated in the first namespace to read the name of the Local Computer and all the IP addresses of the machine. I. Overview: We know that a computer has only one computer name, but it can have multiple IP addresses. For example, when a computer attempts to access the Internet through dial-up, after verifying the user name and password, it will dynamically allocate an IP address. In this case, the computer has two IP addresses, one is the IP address used by the local area network and the other is the IP address dynamically allocated by the dial-up. This article describes how to read the two IP addresses and computer names. II. Environment for program design and operation: (1) Microsoft Windows 2000 Server Edition (2). Net FrameWrok SDK Beta 2 Iii. Main Ideas and implementation methods of Program Design: (1). Read the computer name: In the namespace System. Net, a Dns class is defined, and an important method GetHostName () is defined in this class. The return value of this method is the name of the local computer. In programming, you must first import the System. Net namespace, and then call the GetHostName () method in the Dns class to read the name of the local computer. The main statement for implementation is as follows: Label1.Text = "Host Name:" + System. Net. Dns. GetHostName (); (2). Read the temporary IP address of the computer's dial-up Internet access and the fixed IP address assigned by the LAN: In programming, we read the IP address through a custom function, getIPAddress. First, let's take a look at how to read a local fixed IP address. A Method GetHostByName () is also defined in the Dns class (). When the return value of this method is IPHostEntry, this object has an attribute of AddressList, which is an array of IPAddress type and contains all the IP address information of the computer at this time. This also includes the temporary allocated IP address obtained by dialing the Internet and the fixed IP address of the LAN. The specific implementation statement is as follows: Private static stringgetIPAddress () { System. Net. IPAddress addr; // Obtain the local lan ip Address Addr = new System. Net. IPAddress (Dns. GetHostByName (Dns. GetHostName (). AddressList [0]. Address ); Return addr. ToString (); } The source program is as follows: Using System; Using System. Drawing; Using System. Collections; Using System. ComponentModel; Using System. Windows. Forms; Using System. Data; Using System. Net; Namespace GetIP { Public class Form1: System. Windows. Forms. Form { Private System. Windows. Forms. Label label1; Private System. Windows. Forms. Label label2; Private System. ComponentModel. Container components = null; Public Form1 () { InitializeComponent (); Label1.Text = System. Net. Dns. GetHostName (); Label2.Text = getIpAddress (); } Protected override void Dispose (bool disposing) { If (disposing) { If (components! = Null) { Components. Dispose (); } } Base. Dispose (disposing ); } # Region Windows Form Designer generated code Private void InitializeComponent () { This. label1 = new System. Windows. Forms. Label (); This. label2 = new System. Windows. Forms. Label (); This. SuspendLayout (); This. label1.Location = new System. Drawing. Point (48, 24 ); This. label1.Name = "label1 "; This. label1.Size = new System. Drawing. Size (264, 24 ); This. label1.TabIndex = 0; This. label1.Text = "label1 "; This. label2.Location = new System. Drawing. Point (48, 64 ); This. label2.Name = "label2 "; This. label2.Size = new System. Drawing. Size (264, 24 ); This. label2.TabIndex = 1; This. label2.Text = "label2 "; This. AutoScaleBaseSize = new System. Drawing. Size (6, 14 ); This. ClientSize = new System. Drawing. Size (376,221 ); This. Controls. AddRange (new System. Windows. Forms. Control [] {this. label2, this. label1 }); This. Name = "Form1 "; This. Text = "Form1 "; This. ResumeLayout (false ); } # Endregion Static void Main () { Application. Run (new Form1 ()); } Private static string getIpAddress () { System. Net. IPAddress addr; Addr = new System. Net. IPAddress (Dns. GetHostByName (Dns. GetHostName (). AddressList [0]. Address ); Return addr. ToString (); } } } |