当前位置:网站首页>C#网络应用编程,实验2:IP地址转换和域名解析练习
C#网络应用编程,实验2:IP地址转换和域名解析练习
2022-07-27 17:13:00 【华为云】
实验2:IP地址转换和域名解析练习
1.通过本实验,复习本教材各章例子里的前端主界面和子页面的实现。
2、通过本实验,熟悉和掌握IPAddress类、IPEndPoint类、IPHostEntry类、DNS类的使用。
1、创建一个WPF应用程序项目
2、将App.xaml中的Application.Resources节内容改为

3、修改MainWindow.xaml及代码隐藏类
MainWindow.xaml

MainWindow.xaml.cs主要内容

4、用鼠标右键单击项目,选择【添加】-【新建文件夹】命令,在项目中添加一个名为Examples的文件夹。
5、用鼠标右键单击Examples文件夹,选择【添加】-【页】命令,在该文件夹下添加一个名为Page1.xaml的页。之后再按同样方法添加页Page2.xaml和Page3.xaml。
6、修改Page1.xaml的核心代码

Page1.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.Net;using System.Net.Sockets;namespace 实验1_WPF练习.Examples{ /// <summary> /// Page1.xaml 的交互逻辑 /// </summary> public partial class Page1 : Page { public Page1() { InitializeComponent(); } private void btn_Click(object sender, RoutedEventArgs e) { StringBuilder sb = new StringBuilder(); sb.AppendLine("获取www.cctv.com的所有IP地址:"); try { IPAddress[] ips = Dns.GetHostAddresses("www.cctv.com"); foreach (IPAddress ip in ips) { sb.AppendLine(ip.ToString()); } } catch (Exception ex) { MessageBox.Show(ex.Message, "获取失败"); } string hostName = Dns.GetHostName(); sb.AppendLine("获取本机所有IP地址:"); IPHostEntry me = Dns.GetHostEntry(hostName); foreach (IPAddress ip in me.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { sb.AppendLine("IPv4:" + ip.ToString()); } else if (ip.AddressFamily == AddressFamily.InterNetworkV6) { sb.AppendLine("IPv6:" + ip.ToString()); } else { sb.AppendLine("其他:" + ip.ToString()); } } IPAddress localip = IPAddress.Parse("::1"); //IPv6回路测试地址 Output(sb, localip); IPAddress localip1 = IPAddress.Parse("127.0.0.1");//IPv4回路测试地址 Output(sb, localip1); textBlock1.Text = sb.ToString(); } private static void Output(StringBuilder sb, IPAddress localip) { IPEndPoint iep = new IPEndPoint(localip, 80); if (localip.AddressFamily == AddressFamily.InterNetworkV6) { sb.Append("IPv6端点: " + iep.ToString()); } else { sb.Append("IPv4端点: " + iep.ToString()); } sb.Append(",端口 " + iep.Port); sb.Append(",地址 " + iep.Address); sb.AppendLine(",地址族 " + iep.AddressFamily); } }}结果

边栏推荐
- Detailed interpretation of IEC104 protocol (II) interaction process and protocol analysis
- The go zero singleton service uses generics to simplify the registration of handler routes
- ToggleButton(按钮开关)
- 【深度学习基础知识 - 41】深度学习快速入门学习资料
- Fabric上搭建Hyperledger caliper进行性能测试
- transformers-bert
- Togglebutton (button switch)
- 爱立信承认在中国等五国行贿,向美支付10.6亿美元罚款
- Transaction log full problem handling in sqlserver 2008
- mysql学习录(三)多表查询、子查询、分页查询、case语句、单行函数
猜你喜欢

Intent (whether there is return value to jump)

An unknown fastcgi error occurred in IIS: 0x80070005

ToggleButton(按钮开关)

【深度学习基础知识 - 45】机器学习中常用的距离计算方法
![[basic knowledge of deep learning - 42] detailed explanation of logistic regression](/img/74/d7d1562ada4671864961721b9a1baf.png)
[basic knowledge of deep learning - 42] detailed explanation of logistic regression

Map和Set

JVM概述和内存管理(未完待续)

Hyperledger caliper is built on fabric for performance test

Virtualbox:ssh connection

SQlife(数据库)
随机推荐
Oracle XE版安装与用户操作
王牌代码静态测试工具Helix QAC 2022.2中的新增功能(2)
vlan试验2021.1.14
[basic knowledge of deep learning - 41] quick start learning materials for deep learning
VS2017#include 'xxx.h'
11.5.OSPF
Summenudemo (submenu)
Broadcastreceiver (broadcast)
Dry goods of technical practice | preliminary exploration of large-scale gbdt training
AutoCompleteTextView (input box pre match)
[basic knowledge of deep learning - 48] characteristics of Bayesian network
Virtualbox:ssh connection
揭秘高通超声波指纹被“贴膜破解”之谜
深度主动学习综述2020
【深度学习基础知识 - 49】Kmeans
GestureDetector(手势识别)
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xff in position 0: invalid start byte
FileOutputStream(文件储存)与FileInputStream(文件读取)
【日常积累 - 06】查看cuda和cudnn版本
5W bonus pool / for colleges and universities, 2022 legal science and technology innovation competition is in progress