当前位置: 代码迷 >> ASP.NET >> ASP.NET(C#) ping局域网的其他机子,该如何处理
  详细解决方案

ASP.NET(C#) ping局域网的其他机子,该如何处理

热度:4652   发布时间:2013-02-26 00:00:00.0
ASP.NET(C#) ping局域网的其他机子
我想知道哪局域网中哪些机子开了,在.net(c#)中应该怎么写呢
哪位大哥,有源码.最好简单些,我仅仅是想知道哪些能PING通就行了


------解决方案--------------------------------------------------------
可以去C#版块问问
------解决方案--------------------------------------------------------
C# 获得局域网主机列表实例

  /// 数组初始化
  ///
  private void InitLanHost()
  {
  LanHost = new string[255,2];
  for (int i=0;i <255;i++)
  {
  LanHost[i,0] = " ";
  LanHost[i,1] = " ";
  }
  }

///
  /// 局域网搜索事件
  ///
  private void LanSearch()
  {
  thread = new Thread[255];
  
  ThreadStart threadMethod;
  
  Thread threadProgress = new Thread(new ThreadStart(progressSearch));
  threadProgress.Start();
  
  string localhost = (Dns.GetHostByName(Dns.GetHostName())).AddressList[0].ToString(); //本地主机IP地址
  str = localhost.Substring(0,localhost.LastIndexOf( ". "));
  
  for (int i=0;i <255;i++) //建立255个线程扫描IP
  {
  threadMethod = new ThreadStart(LanSearchThreadMethod);
  thread[i] = new Thread(threadMethod);
  thread[i].Name = i.ToString();
  thread[i].Start();
  if (!thread[i].Join(100))  //Thread.Join(100)不知道这处这么用对不对,感觉没什么效果一样
  {
  thread[i].Abort();
  }
  }
  
  GetLanHost();
  listLanHost();
  }
  ///
  /// 多线程搜索方法
  ///
  private void LanSearchThreadMethod()
  {
  int Currently_i = Convert.ToUInt16(Thread.CurrentThread.Name); //当前进程名称
  
  IPAddress ScanIP = IPAddress.Parse( str + ". "+Convert.ToString(Currently_i +1)); //获得扫描IP地址
  IPHostEntry ScanHost = null;
  ScanHost = Dns.GetHostByAddress(ScanIP);  //获得扫描IP地址主机信息
  
  if (ScanHost != null)
  {
  LanHost[Currently_i,0] = ScanIP.ToString();
  LanHost[Currently_i,1] = ScanHost.HostName;
  }
  
  //progressBarSearch.Value = progressBarSearch.Value +1;
  
  }
  ///
  /// 文本框显示主机名与IP列表
  ///
  private void GetLanHost()
  {
  for (int i=0;i <255;i++)
  if ( LanHost[i,0] != " ")
  {
  textBox1.Text =textBox1.Text + LanHost[i,1] + ": " +LanHost[i,0] + "\r\n ";
  }
  }
  ///
  /// listview1 显示搜索主机
  ///
  private void listLanHost()
  {
  listView1.View = View.List;
  
  ListViewItem aa ;
  for (int i=0;i <255;i++)
  {
  if ( LanHost[i,0] != " ")
  {
  aa= new ListViewItem();
  aa.Text = LanHost[i,1];
  aa.Tag = LanHost[i,0];
  listView1.Items.Add(aa);
  }
  }
  
  }
  ///
  /// 进度条处理线程
  ///
  private void progressSearch()
  {
  //label1.Text = "进度条只是时间估计,不是真实搜索进度! ";
  progressBarSearch.Value = 0;
  for (int i=0;i <255;i++)
  {
  progressBarSearch.Value = progressBarSearch.Value + 1;
  Thread.Sleep(100);
  }
  }
  }
  }


------解决方案--------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.NetworkInformation;
using System.Collections;
using System.Threading;

namespace NetKnocker
{
public partial class Knocker : Form
{
private Thread bgThread = null;
private Thread tmpThread = null;
private ArrayList bgThreads = null;