本人最近做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 =&#