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;
arResult[15] = (byte)(timestamp > > 8);
arResult[16] = (byte)(timestamp > > 16);
arResult[17] = (byte)(timestamp > > 24);
arResult[18] = (byte)(timestamp > > 32);
arResult[19] = (byte)(timestamp > > 40);
arResult[20] = (byte)(timestamp > > 48);
arResult[21] = (byte)(timestamp > > 56);
return arResult;
}
}
public class CDWPing : IDWPing
{
private int m_Timeout; //in msec
private int[] m_arTime; //response times statistic
private bool[] m_arResults; //results of pings
private byte[] m_arTTL; //routing stations statistic
private int m_idxPing;
private string m_strErrorMessage;
//----------------------------------------------------
public int Timeout { set{ m_Timeout = MinMax.max(value, 1); }}
public int Repeats { set
{ int n = MinMax.max(value, 1);
m_arTime = new int[n];
m_arTTL = new byte[n];
m_arResults = new bool[n];
}
}
public int AvgTime { get{ return this.calcAvgTime(); }}
public int AvgTTL { get{ return this.calcAvgTTL(); }}
public string ErrorMessage { get{ return m_strErrorMessage; }}
public CDWPing()
{
m_arTime = new int[1]; //size of m_arTime is number of tries
m_arTTL = new Byte[1];
m_arResults = new bool[1];
m_strErrorMessage = "Don 't know what happened ";
m_Timeout = 500; //msec
}
public short ping(string strHostName)
{
m_strErrorMessage = "No error occured ";
this.clearStats();
short result = 0;
//convert strHostName to an IPEndPoint
try
{
IPEndPoint lep;
//check if strHostname is already a dotted address
//and create an IPEndpoint from it
//note: port 7 is for echo but is irrelevant because of the socket type we are going to use
const int echoPort = 7;
if(true == this.isIPAddress(strHostName))
{
IPAddress ipAddr = IPAddress.Parse(strHostName);
lep = new IPEndPoint(ipAddr, echoPort);
}
else
{
IPHostEntry lipa = Dns.Resolve(strHostName);
lep = new IPEndPoint(lipa.AddressList[0], echoPort);
}
//number of tries
for(m_idxPing = 0; m_idxPing < m_arTime.Length; m_idxPing++)
{
if(true == tryPing(lep))
{
m_arResults[m_idxPing] = true;
result = 1; //one successful ping = overall success
}
else
{
m_arResults[m_idxPing] = false;
}
}
}
catch(SocketException ex)
{
result = 0;
m_strErrorMessage = ex.Message;
}
catch(Exception ex )
{
result = 0;
m_strErrorMessage = ex.Message;
}
return result;
}
[解决办法]
/*检查连接网络情况
p_strIP - 服务器ip地址,
p_intPort - 待检测服务器端口号
*/
private static bool checkNetwork(string p_strIP,int p_intPort)
{
bool isReady = false;//返回值(网络是否通畅)
IPAddress ip = IPAddress.Parse(p_strIP);
IPEndPoint ipes = new IPEndPoint(ip, p_intPort);
Socket sockets = new Socket(ipes.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try
{
sockets.Connect(ipes);//连接到指定地址簇
if (sockets.Connected)//获取连接状态
isReady = true;
}
catch (Exception e)
{
//@连接错误
}
return isReady;
}
------解决方案--------------------
如下试试看:
public static void SimplePing ()
{
Ping pingSender = new Ping ();
PingReply reply = pingSender.Send (IPAddress);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine ( "Address: {0} ", reply.Address.ToString ());
Console.WriteLine ( "RoundTrip time: {0} ", reply.RoundtripTime);
Console.WriteLine ( "Time to live: {0} ", reply.Options.Ttl);
Console.WriteLine ( "Don 't fragment: {0} ", reply.Options.DontFragment);
Console.WriteLine ( "Buffer size: {0} ", reply.Buffer.Length);
}
else
{
Console.WriteLine (reply.Status);
}
}
[解决办法]
如是2003用C#调cmd--ping, 是2005就更好办了直接用Ping了。
[解决办法]
你的服务器是不是IP地址不固定?
[解决办法]
API里的ping^……
[解决办法]
学习.
[解决办法]
比较简单的方法是创建一个webclient对象,连接一个比较大的网站,如果能连接上,就是通的。
[解决办法]
API的PING下