网络应用编程 Chapter1

目录

 

 

例1-1 DNS域名解析和IP地址转换的基本用法

例1-2 获取网络适配器信息

例1-3 网络流量统计


 

ScrollViewer:滚动视图
strokedasharray属性:用于创建虚线
NavigationUIVisibility="Hidden" :隐藏菜单方向键
TextBlock.LineHeight:改变行距
TextWrapping="Wrap":表示当文本长度超过容器长度时可以自动换行

这里只放了最核心的Page后台类文件的代码。注意命名空间的引用。

 

例1-1 DNS域名解析和IP地址转换的基本用法

public partial class DnsExamplePage : Page
    {
        public DnsExamplePage()
        {
            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());
                }
            }
            //IPv6回路测试地址
            IPAddress localip = IPAddress.Parse("::1");
            Output(sb, localip);

            //IPv4回路测试地址
            IPAddress localip1 = IPAddress.Parse("127.0.0.1");
            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);
        }
    }

 

例1-2 获取网络适配器信息

 

/// <summary>
    /// NetworkInterfacePage.xaml 的交互逻辑
    /// </summary>
    public partial class NetworkInterfacePage : Page
    {
        public NetworkInterfacePage()
        {
            InitializeComponent();
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            sb.AppendLine("适配器个数:" + adapters.Length);
            int index = 0;
            foreach (NetworkInterface adapter in adapters)
            {
                index++;
                //显示网络适配器描述信息、名称、类型、速度、MAC地址
                sb.AppendLine("-----------------第" + index + "个适配器信息-------------------");
                sb.AppendLine("描述信息:" + adapter.Description);
                sb.AppendLine("名称:" + adapter.Name);
                sb.AppendLine("类型:" + adapter.NetworkInterfaceType);
                sb.AppendLine("速度:" + adapter.Speed / 1000 / 1000 + "M");
                byte[] macBytes = adapter.GetPhysicalAddress().GetAddressBytes();
                sb.AppendLine("MAC地址:" + BitConverter.ToString(macBytes));

                //获取IPInterfaceProperties实例
                IPInterfaceProperties adapterProperties = adapter.GetIPProperties();

                //获取并显示DNS服务器IP地址信息
                IPAddressCollection dnsServers = adapterProperties.DnsAddresses;
                if (dnsServers.Count > 0)
                {
                    foreach (IPAddress dns in dnsServers)
                    {
                        sb.AppendLine("DNS服务器IP地址:" + dns);
                    }
                }
            }
            textBlock1.Text = sb.ToString();
        }
    }

 

例1-3 网络流量统计

/// <summary>
    /// IPGlobalStaticsPage.xaml 的交互逻辑
    /// </summary>
    public partial class IPGlobalStaticsPage : Page
    {
        public IPGlobalStaticsPage()
        {
            InitializeComponent();
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
            IPGlobalStatistics ipstat = properties.GetIPv4GlobalStatistics();
            sb.AppendLine("本机注册域名 : " + properties.DomainName);
            sb.AppendLine("接收数据包数 : " + ipstat.ReceivedPackets);
            sb.AppendLine("转发数据包数 : " + ipstat.ReceivedPacketsForwarded);
            sb.AppendLine("传送数据包数 : " + ipstat.ReceivedPacketsDelivered);
            sb.AppendLine("丢弃数据包数 : " + ipstat.ReceivedPacketsDiscarded);
            textBlock1.Text = sb.ToString();
        }
    }

 

 

 

PS:

写代码和找bug的时间比1:5     害,就每天写bug呗...憨憨落泪

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值