C#如何监测某台机器网络连接是否畅通?

jhdxhj 2008-12-17 12:38:25
C#如何监测某台机器网络连接是否畅通?
...全文
231 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hudi165 2008-12-17
  • 打赏
  • 举报
回复
如果是判断本机是否连通的话,可以调用API
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
/// <summary>
/// 获取机器是否连接上网络
/// </summary>
public static bool IsConnected
{
get
{
int Desc;
return InternetGetConnectedState(out Desc, 0);
}
}
如果是判断其他的机器是否能连上本机,可以用Ping这个类
Ping pingServer = new Ping();
PingReply reply = pingServer.Send("目标机器的名称或ip");
if (reply.Status == IPStatus.Success)
{
//连接成功
}
ttg520 2008-12-17
  • 打赏
  • 举报
回复
Ping pingSender = new Ping();
PingOptions options = new PingOptions();

// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
options.DontFragment = true;

// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
PingReply reply = pingSender.Send("222.111.111.111", timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
i = 0;
Console.WriteLine("IP地址: {0}", reply.Address.ToString());
Console.WriteLine("返回时间: {0}", reply.RoundtripTime);
}
看返回时间
jhdxhj 2008-12-17
  • 打赏
  • 举报
回复
高人呢?
pxqsu 2008-12-17
  • 打赏
  • 举报
回复
这一方面我有一点理解,不过没做过。可以用PING机制。它只能检测连通。具体就用Socket class中的raw socket功能,发送一个ICMP报文。
jhdxhj 2008-12-17
  • 打赏
  • 举报
回复
难道就没有一个人会这东西吗?

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧