程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#關於判斷網絡連接正常與否的總結

c#關於判斷網絡連接正常與否的總結

編輯:C#入門知識

本人最近做c#winform的項目,遇到了判斷網絡是否正常連接的問題。後來查出了以下幾種方法,供大家學習參考。
1.方法一
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
namespace WindowsFormsApplication1
{
    public partial class Demo : Form
    {
        public Demo()
        {
            InitializeComponent();
        }
        //判斷
        private void btpanduan_Click(object sender, EventArgs e)
        {
            //210.192.120.228  163網易
            string ip = this.txtip.Text.ToString();
            int port = Convert .ToInt32( this.txtport.Text.ToString());
            bool a = panduan(ip, port );//135為本機服務端口號
            if (a == true)
            {
                MessageBox.Show("該網絡連接正常 !");
            }
            else
            {
                MessageBox.Show("該網絡連接不暢通 !");
            }
        }
      
      // 異步調用
 
        //判斷的方法
        public bool panduan(string ip, int port)
        {
            try
            {
                TcpClient client = new TcpClient(ip, port);
                if (client.Connected)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
 
2.利用 c# ping類
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
namespace WindowsFormsApplication1
{
    public partial class Demo3 : Form
    {
        public Demo3()
        {
            InitializeComponent();
        }
        System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
        System.Net.NetworkInformation.PingReply res;
        //檢查網絡連接
        private void btcheck_Click(object sender, EventArgs e)
        {

            string url = this.txturl.Text.ToString();
            bool a = check(url);
            if (a == true)
            {
                MessageBox.Show("連接成功!", "提示信息");

            }
            else
            {
                MessageBox.Show("連接失敗!", "提示信息");
            }
        }

        public bool check(string url)
        {

            try
            {
                res = ping.Send(url);
                if (res.Status == System.Net.NetworkInformation.IPStatus.Success)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch {

                return false;
            }
        }
    }
}
 


摘自 wanglei_smartfish的專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved