获得主机域名及其IP和Port端口

主程序代码:

 

   // 由主机域名获得其IP地址
    protected void btnShowLocal_Click(object sender, EventArgs e)
    {
        txtaShowAnswer.Value = "";
        string strShowAnwser = string.Empty;

        string strHostName = Dns.GetHostName(); //获取本地主机名
        strShowAnwser = "The local host's name is: " + strHostName + "/n";
        IPHostEntry ipHost = Dns.GetHostEntry(strHostName); //将主机名解析成IPHostEntry实例
        foreach (IPAddress ip in ipHost.AddressList) //将主机名(域名)对应的IP全部解析出来
            strShowAnwser += "The local host's IP is: " + ip.ToString() + "/n";
        IPAddress LocalIP = IPAddress.Parse("127.0.0.1"); //将字符串实例成IP地址
        IPEndPoint ipEP = new IPEndPoint(LocalIP, 80); //将网络端点表示成IP地址和端口号
        strShowAnwser += "The IPEndPoint is: " + ipEP.ToString() + "/n";
        strShowAnwser += "The Address is: " + ipEP.Address + "/n";
        strShowAnwser += "The Port is: " + ipEP.Port + "/n";
        strShowAnwser += "The AddressFamily is: " + ipEP.AddressFamily + "/n";
        strShowAnwser += "The Max port number is: " + IPEndPoint.MaxPort + "/n";
        strShowAnwser += "The Min port number is: " + IPEndPoint.MinPort + "/n";

        txtaShowAnswer.Value = strShowAnwser;
    }

 

 

运行结果:

The local host's name is: Young
The local host's IP is: ::1
The local host's IP is: 210.77.29.132
The IPEndPoint is: 127.0.0.1:80
The Address is: 127.0.0.1
The Port is: 80
The AddressFamily is: InterNetwork
The Max port number is: 65535
The Min port number is: 0

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值