C#网络应用编程

您所在的位置:网站首页 网络电视无法解析域名 C#网络应用编程

C#网络应用编程

2024-07-10 19:09| 来源: 网络整理| 查看: 265

熟悉和掌握IPAddress类、IPEndPoint类、IPHostEntry类、DNS类的使用

使用IPAddress类、IPEndPoint类、IPHostEntry类、DNS类的方法,显示中央电视台所有服务器的IP地址信息和本机主机名及相关IP地址。

重点:IPAddress类、IPEndPoint类、IPHostEntry类、DNS类方法和属性的正确使用。

难点:无

MainWindow.xaml

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); } } }

其中

实现了按钮点击过后颜色的转变;

实现了界面的跳转。

App.xaml

设置了Label和Border的格式供给Dnspage调用。

DnsPage.xaml 

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); } } }

实验运行截图

仅供参考 本人盒带21届小趴菜 切勿直接复制 查重难办QAQ



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3