局域网电脑掉线检测c#编程问题

野_草 2007-08-03 09:12:22
单位有一个几十台电脑的封闭局域网络,win2000操作系统。个别电脑偶尔掉线,如果一台电脑做服务端,监测哪台电脑掉线,其他电脑不做任何变动,如何入手C#编程?我接触Socket比较少,从何入手啊? 高手帮忙!
ping可以吗?是否效率太低啊?侦听网络上的数据包可以吗?能否从服务端与每台电脑建立连接通路?有朋友说:“UDP 发数据 出错 说明无法连接”。哪种方案好啊?关键的c#代码能示例一些吗?

...全文
472 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
野_草 2007-08-28
  • 打赏
  • 举报
回复
完工了!
边城的刀声 2007-08-09
  • 打赏
  • 举报
回复
mark
野_草 2007-08-09
  • 打赏
  • 举报
回复
试过了,比较好。谁解释一下异步使用 Ping 类和上一个ping的区别啊。还有就是30多台机器,应该使用多线程了吗?怎么实现线程啊?忙里偷闲,也不知道什么时候完成。不过我会把最后的代码和大家讨论的。
野_草 2007-08-09
  • 打赏
  • 举报
回复
异步使用 Ping 类:
using System;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;
using System.ComponentModel;
using System.Threading;

namespace Examples.System.Net.NetworkInformation.PingTest
{
public class PingExample
{
public static void Main (string[] args)
{
if (args.Length == 0)
throw new ArgumentException ("Ping needs a host or IP Address.");

string who = args[0];
AutoResetEvent waiter = new AutoResetEvent (false);

Ping pingSender = new Ping ();

// When the PingCompleted event is raised,
// the PingCompletedCallback method is called.
pingSender.PingCompleted += new PingCompletedEventHandler (PingCompletedCallback);

// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);

// Wait 12 seconds for a reply.
int timeout = 12000;

// Set options for transmission:
// The data can go through 64 gateways or routers
// before it is destroyed, and the data packet
// cannot be fragmented.
PingOptions options = new PingOptions (64, true);

Console.WriteLine ("Time to live: {0}", options.Ttl);
Console.WriteLine ("Don't fragment: {0}", options.DontFragment);

// Send the ping asynchronously.
// Use the waiter as the user token.
// When the callback completes, it can wake up this thread.
pingSender.SendAsync(who, timeout, buffer, options, waiter);

// Prevent this example application from ending.
// A real application should do something useful
// when possible.
waiter.WaitOne ();
Console.WriteLine ("Ping example completed.");
}

public static void PingCompletedCallback (object sender, PingCompletedEventArgs e)
{
// If the operation was canceled, display a message to the user.
if (e.Cancelled)
{
Console.WriteLine ("Ping canceled.");

// Let the main thread resume.
// UserToken is the AutoResetEvent object that the main thread
// is waiting for.
((AutoResetEvent)e.UserState).Set ();
}

// If an error occurred, display the exception to the user.
if (e.Error != null)
{
Console.WriteLine ("Ping failed:");
Console.WriteLine (e.Error.ToString ());

// Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
}

PingReply reply = e.Reply;

DisplayReply (reply);

// Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
}

public static void DisplayReply (PingReply reply)
{
if (reply == null)
return;

Console.WriteLine ("ping status: {0}", reply.Status);
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);
}
}
}
}
野_草 2007-08-09
  • 打赏
  • 举报
回复
haojie860兄弟的代码基于域名解析的,有时候不能及时反应掉线情况。我查了一些资料和MSDN文档,觉得还是ping的方式比较好实现。 把我的代码放在这:
msdn上的ping:
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;

namespace Examples.System.Net.NetworkInformation.PingTest
{
public class PingExample
{
// args[0] can be an IPaddress or host name.
public static void Main (string[] args)
{
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 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);
int timeout = 120;
PingReply reply = pingSender.Send (args[0], timeout, buffer, options);
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);
}
}
}
}
wzd24 2007-08-04
  • 打赏
  • 举报
回复
让服务端向全网发出广播,客户端在收到广播后回复信息,如果哪台机器没有回复说明它掉线了!

或者让客户端定时向服务器发出信息,如果服务器在指定的时间内没有收到信息,就判定这台机器掉线!
sd5923150 2007-08-04
  • 打赏
  • 举报
回复
客户端实现掉线??
是说连向外网的时候掉线检测吗?
如果是要直接在服务端编程。应该是
在客户端放检测代码,在服务端运行吧??
haojie860 2007-08-04
  • 打赏
  • 举报
回复
试试,这个呢?

using System.Threading; //线程命名空间
using System.Net; //网络命名空间
//思想为获取IP地址的主机名与信息.如发生异常,则说明主机不可到达(关机)

private void scan()
{
listView1.Items.Clear();
ListViewItem listviewitem; //清空listView1的项
string strIPaddress = numericUpDown1.Text + "." + numericUpDown2.Text + "." + numericUpDown3.Text + "."; //或取前24位的IP地址
int start = Int32.Parse(numericUpDown4.Text);
int end = Int32.Parse(numericUpDown5.Text);
if (start > end)
MessageBox.Show("终止地止必须大于启始地址", "警告"); //比较开始地址与接束地址,不允许终止地止必须小于启始地址
else
{
for (int i = start; i < end; i++)
{
string strscanip = strIPaddress + i.ToString();//构造IP地址
IPAddress sacanip = IPAddress.Parse(strscanip);//转换成IP地址

listviewitem = new ListViewItem(strscanip, 0);//在ListViewItem添加IP地址
try
{
IPHostEntry hostinfo = Dns.GetHostByAddress(sacanip); //根据ip地址或取主机的DNS信息
string hostname = hostinfo.HostName.ToString(); //或取主机名

listviewitem.SubItems.Add(hostname); //在ListViewItem添加主机名
listviewitem.SubItems.Add("可到达");
}
catch (Exception ew)
{
listviewitem.SubItems.Add("未知");
listviewitem.SubItems.Add("不可到达");
}
listView1.Items.Add(listviewitem); //将项加入listView1
}
}
}
野_草 2007-08-04
  • 打赏
  • 举报
回复
客户端不允许做任何变动,只能在服务端编程。xuyiazl你好!能否说详细一点,如何用TCP异步检测
vainnetwork 2007-08-04
  • 打赏
  • 举报
回复
关注,帮顶了............
xuyiazl 2007-08-04
  • 打赏
  • 举报
回复
可以像arp那样~
或者说是每格段时间ping一次
用TCP异步检测~
xwk789xwk 2007-08-04
  • 打赏
  • 举报
回复
对这方面不是很懂,只能帮顶了
huyejun 2007-08-04
  • 打赏
  • 举报
回复
UDP是无连接的 用TCP吧
QuinsonYue 2007-08-04
  • 打赏
  • 举报
回复
up
liusong_china 2007-08-03
  • 打赏
  • 举报
回复
关注
帮顶一下

110,534

社区成员

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

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

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