C#中如何知道 网络 通还是不通
C#中如何知道 网络 通还是不通???
并不是 要知道能不能 上网这个所谓,那是路由器封的!!!如何知道 网络现在 通不通???
------解决方案--------------------可以 有个api 我找下
------解决方案--------------------不能用ping吗?直接写个ping 类
------解决方案--------------------以前是用API里的ping功能
现在c#应该封装得有ping功能,找找看。
检测是否连接上交换机,ping自己IP就可以。
是否连接上服务器,ping服务器啊
下面代码来自:http://www.iters.cn/html/2005-12/85.htm
C# Ping
By Gerhard Schmeusser
using System;
using System.Net;
using System.Diagnostics;
using System.Net.Sockets;
using System.Collections;
namespace cmpDWPing
{
///
//Implements an Ethernet Ping component
//Ping is part of the ICMP (Internet Control Message Protocol) which serves
//Hosts and Routers for the purpose of error messaging and exchanging status and commands.
//It is part of the IP protocol and therefore cannot be considered as reliable.
//The following ICMP messages are used here to implement a ping functionality:
//Echo Request
//Echo Answer
//The interface is pretty straightforward:
//the ping method performs the network ping and returns success or failure
//in case of failure ask the ErrorMessage property for the reason
//
// There are some properties which you can use for setting and getting information. See the interface below
//
//
//
//
//You can use this component as a COM object if
//you create a COM callable wrapper (CCW) by the following command line programs:
//the typelib is created by the project setting "register for COM interop ":
//regasm bin/debug/cmpDWPing.dll
//if you also want to manually create a typelib use the following line
//regasm /tlb:cmpDWPing.tlb bin/debug/cmpDWPing.dll
//use the following lines to register the component in the global assembly cache
//gacutil /u cmpDWPing
//gacutil /i bin/debug/cmpDWPing.dll
//
//Beforehand justification: Because of the C-style design of the ICMP protocol data structures
//which are meant to be cast this way and that and therefore not very
//suitable to a strict language like C#, we have to
//adopt some pretty awkward code here.
///
interface IDWPing
{
short ping(string strHostName); // 1 = success, 0 = failure
int Timeout {set;} //default = 500msec
int Repeats {set;} //default = 0
int AvgTime {get;} //measured average response time
int AvgTTL {get;} //measured average number of routing nodes the ping traveled
string ErrorMessage {get;} //a verbose error message in case of failure
}
struct ICMPHeader
{
public byte type;
public byte code;
public ushort chksum;
public ushort id;
public ushort seq;
public ulong timestamp;
public byte[] toByteArray()
{
//If you know a better way to serialize this into a byte array, let me know
byte[] arResult = new byte[22];
arResult[0] = this.type;
arResult[1] = this.code;
arResult[2] = (byte)chksum;
arResult[3] = (byte)(chksum > > 8);
arResult[4] = (byte)(chksum > > 16);
arResult[5] = (byte)(chksum > > 24);
arResult[6] = (byte)id;
arResult[7] = (byte)(id > > 8);
arResult[8] = (byte)(id > > 16);
arResult[9] = (byte)(id > > 24);
arResult[10] = (byte)seq;
arResult[11] = (byte)(seq > > 8);
arResult[12] = (byte)(seq > > 16);
arResult[13] = (byte)(seq > > 24);
arResult[14] = (byte)timestamp;