zoukankan      html  css  js  c++  java
  • 获得计算机名称和IP地址(转)

    获得计算机名称和IP地址
    源作者:追风                   人气:9779

    VisualC#是微软公司推出的下一代程序开发语言,是微软.Net 框架中的的一个重要组成部分,在推出VisualC#的过程中,微软公司还推出了与之相对应的一个软件开发包--.Net FrameWorkSDK。此软件开发包里面封装了许多类、对象。Visual C#就是通过调用这些类、对象来实现许多比较强大的功能。
    在.Net FrameWork SDK中提供了二个可用于网络编程的名称空间,一个是System.Net,另一个是System..Net.Socket。本文就是利用第一个名称空间中封装的类和对象来读取本地计算机名称和机器中所有的IP地址。
    一.概述:
    我们知道对于一台计算机来说,他只有一个计算机名称,但是他可以有多个IP地址。例如当计算机通过拨号上网的时候,在验证完用户名和口令以后,就会动态分配一个IP地址,此时计算机就拥有了二个IP地址,一个时自己设定的局域网用的IP地址,另外一个就是拨号上网动态分配的IP地址了。本文就是来探索一下如何读取此二个IP地址和计算机名称。
    二.程序设计和运行的环境:
    (1)微软公司视窗2000服务器版
    (2).Net FrameWrok SDK Beta 2版
    三.程序设计的主要思路及实现方法:
    (1).读取计算机的名称:
    在名称空间System.Net中定义了一个类Dns,在此类中定义了一个比较重要的方法 GetHostName (),此方法的返回值就是本地计算机名称。在程序设计中首先要导入System.Net名称空间,然后通过调用Dns类中的GetHostName ()方法读取本地计算机名称,具体实现的主要语句如下:
    label1.Text = "主机名称:" + System.Net.Dns.GetHostName ( ) ;
    (2).读取计算机的拨号上网临时的IP地址和局域网分配的固定IP地址:
    在程序设计中,我们是通过一个自定义的函数--getIPAddress ( )来读取IP地址的。首先看一下如何读取本地固定的IP地址的。在Dns类中还定义了一个方法GetHostByName( )。此方法的返回值时IPHostEntry 对象,此对象有一个属性是AddressList,此属性是一个IPAddress类型的数组,包含了计算机此时的所有的IP地址信息。这当中也就包含了拨号上网得到的临时分配的IP地址和局域网固定的IP地址。具体实现语句如下:
    private static stringgetIPAddress ( )
    {
    System.Net.IPAddress addr;
    // 获得本机局域网IP地址
    addr = new System.Net.IPAddress ( Dns.GetHostByName (Dns.GetHostName ( ) ) .AddressList [0].Address ) ;
    return addr.ToString ( ) ;
    }
    源程序如下:
    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();
      }
     }
    }

    作者:KKcat
        
    个人博客:http://jinzhao.me/
        
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    107. Binary Tree Level Order Traversal II
    108. Convert Sorted Array to Binary Search Tree
    111. Minimum Depth of Binary Tree
    49. Group Anagrams
    使用MALTAB标定实践记录
    442. Find All Duplicates in an Array
    522. Longest Uncommon Subsequence II
    354. Russian Doll Envelopes
    opencv 小任务3 灰度直方图
    opencv 小任务2 灰度
  • 原文地址:https://www.cnblogs.com/jinzhao/p/1247344.html
Copyright © 2011-2022 走看看