熟悉和掌握IPAddress类、IPEndPoint类、IPHostEntry类、DNS类的使用
使用IPAddress类、IPEndPoint类、IPHostEntry类、DNS类的方法,显示中央电视台所有服务器的IP地址信息和本机主机名及相关IP地址。
![](https://img-blog.csdnimg.cn/76ddf9b481fe42bf88e98a9edb934048.png)
重点:IPAddress类、IPEndPoint类、IPHostEntry类、DNS类方法和属性的正确使用。
难点:无
MainWindow.xaml
![](https://img-blog.csdnimg.cn/e8df0117188a470f91cc011dcfaa031f.png)
MainWindow.xaml.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;
namespace WpfApp2
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
Button oldbutton=new Button();
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button btn=e.Source as Button;
btn.Foreground = Brushes.Red;
oldbutton.Foreground = Brushes.Black;
oldbutton = btn;
frame_1.Source=new Uri(btn.Tag.ToString(), UriKind.Relative);
}
}
}
其中
![](https://img-blog.csdnimg.cn/4babf2bfdaf54df2a197412e45620d3d.png)
实现了按钮点击过后颜色的转变;
![](https://img-blog.csdnimg.cn/23bc2d0603dc43048a8827215f526020.png)
实现了界面的跳转。
App.xaml
设置了Label和Border的格式供给Dnspage调用。
DnsPage.xaml
![](https://img-blog.csdnimg.cn/d0225bec28374d0c94f3e703e39db903.png)
DnsPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
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;
namespace WpfApp2
{
///
/// DnsPage.xaml 的交互逻辑
///
public partial class DnsPage : Page
{
public DnsPage()
{
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,"获取失败......");
}
sb.AppendLine("获取本机所有IP地址:");
IPHostEntry me = Dns.GetHostEntry("");
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");
Output(localip,sb);
IPAddress localip1 = IPAddress.Parse("127.0.0.1");
Output(localip1,sb);
textblock1.Text = sb.ToString();
}
private static void Output(IPAddress localip,StringBuilder sb)
{
IPEndPoint iPEnd = new IPEndPoint(localip, 80);
if(localip.AddressFamily ==AddressFamily.InterNetworkV6)
{
sb.Append("IPv6端点:" + iPEnd.ToString());
}
else if (localip.AddressFamily == AddressFamily.InterNetwork)
{
sb.Append("IPv4端点:" + iPEnd.ToString());
}
sb.Append(",端口:" + iPEnd.Port);
sb.Append(",地址:" + iPEnd.Address);
}
}
}
实验运行截图
![](https://img-blog.csdnimg.cn/7fe13c32d3c9481f8f43afeae9be29c8.png)
![](https://img-blog.csdnimg.cn/7909faea854c4a449d8c90df0434ca59.png)
![](https://img-blog.csdnimg.cn/7864d7d764af4a6380b4bc3af6091520.png)
仅供参考 本人盒带21届小趴菜 切勿直接复制 查重难办QAQ
|