c#关于判断网络连接正常与否的总结
来源:互联网 发布:参加网络研修所思所想 编辑:程序博客网 时间:2024/04/30 04:34
本人最近做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;
}
}
}
}
- c#关于判断网络连接正常与否的总结
- C#判断网络连接是否正常
- C# 判断连接网络是否正常
- 少女月经正常与否的判断方法
- Windows Mobile判断网络连接与否代码
- android判断网络的状态断开与否
- 判断网络的状态断开与否
- android判断网络的状态断开与否
- 判断网络是否连接正常
- 判断网络是否连接正常
- 判断网络连接是否正常
- 判断网络是否连接正常
- PPC Windows Mobile判断网络(gprs)连接与否代码
- C# 判断网络的连接状态
- C#中判断网络连接的状态
- C#中判断网络连接的状态
- C#中判断网络连接的状态
- [ios开发]判断网络连接是否正常
- form和table的区别
- ACE编译问题
- HTTP 错误
- 按如下格式打印杨辉三角形的前10行。
- drawable保存的sd卡
- c#关于判断网络连接正常与否的总结
- VC里引用第三方库常遇到的链接重复定义问题
- C# JSON串和对象互相转换
- Android 中的 LayoutInflater类
- 设计模式-模板方法
- 编程实现一个栈结构,用数组来实现,具有入栈、出栈操作。
- Android official compile guide
- 关于router name 的url重写 --frontname rewrite frontname重写!
- 外键的作用