網絡應用編程 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唄...憨憨落淚

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章