读书人

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

发布时间: 2012-01-20 18:53:53 作者: rapoo

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;


private Queue IPQueue = null;
private int stoppedThreads = 0;

private Hashtable succeedIPlist = null;
private Hashtable failIPlist = null;

private int max_IP=255;
private int min_IP=0;

private delegate void ShowText(string text);
private ShowText ShowTextDelegate;

private void ShowTextMethod(string text)
{
rtb_Show.AppendText(text);
}


public Knocker()
{
InitializeComponent();
}

private void BgPingThreadMethod()
{
IPAddress ip= (IPAddress)IPQueue.Dequeue();

string tmpip = ip.ToString();

Ping ping = new Ping();
PingReply reply = null;


try
{
reply = ping.Send(ip, 1000);
}
catch (Exception tmpe)
{
failIPlist[tmpip] = tmpe.Message;
}
finally
{
if (reply != null)
if (reply.Status == IPStatus.Success)
succeedIPlist[tmpip]=Dns.GetHostEntry(ip).HostName;
else
failIPlist[tmpip] = reply.Status.ToString();
}

stoppedThreads++;
}

private void BgThreadMethod()
{
string localhostName = Environment.MachineName;
IPAddress ip = Dns.GetHostEntry(localhostName).AddressList[0];

byte [] tmpParts = ip.GetAddressBytes();
string baseIp = tmpParts[0].ToString()+ ". "+tmpParts[1].ToString()+ ". "+tmpParts[2].ToString()+ ". ";

IPAddress tmpip = new IPAddress (0);

for (int i = min_IP; i < max_IP + 1; i++)
{
string tmpstr = baseIp + i.ToString();

tmpip = IPAddress.Parse(tmpstr);

IPQueue.Enqueue(tmpip);


}

for (int i = min_IP; i < max_IP + 1; i++)
{
tmpThread = new Thread(new ThreadStart(BgPingThreadMethod));
tmpThread.IsBackground = true;
tmpThread.Priority = ThreadPriority.BelowNormal;
tmpThread.Start();
bgThreads.Add(tmpThread);
}

while (stoppedThreads != bgThreads.Count)
{
Thread.Sleep(200);
}


if (succeedIPlist.Count > 0)
{
this.Invoke(this.ShowTextDelegate, new object[] { succeedIPlist.Count.ToString()+ " IP addresses can be pinged successfully\n " });

ArrayList tmplist = new ArrayList(succeedIPlist.Keys);
tmplist.Sort();

foreach (string tmpstr in tmplist)
this.Invoke(this.ShowTextDelegate, new object[] { tmpstr + "\t "+succeedIPlist[tmpstr].ToString()+ "\n " });
}

if (failIPlist.Count > 0)
{
this.Invoke(this.ShowTextDelegate, new object[] { "IP addresses can 't be pinged\n "});

ArrayList tmplist = new ArrayList(failIPlist.Keys);
tmplist.Sort();

foreach (string tmpstr in tmplist)
this.Invoke(this.ShowTextDelegate, new object[] { tmpstr + "\t " + failIPlist[tmpstr].ToString() + "\n " });


}
}

private void Knocker_Load(object sender, EventArgs e)
{

ShowTextDelegate = new ShowText(ShowTextMethod);

bgThreads = new ArrayList();
IPQueue = new Queue();
succeedIPlist = new Hashtable();
failIPlist = new Hashtable();

bgThread = new Thread(new ThreadStart(BgThreadMethod));
bgThread.IsBackground = true;
bgThread.Priority = ThreadPriority.BelowNormal;
bgThread.Start();
}
}
}
[解决办法]
namespace NetKnocker
{
partial class Knocker
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name= "disposing "> 如果应释放托管资源,为 true;否则为 false。 </param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.rtb_Show = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// rtb_Show
//
this.rtb_Show.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.rtb_Show.Location = new System.Drawing.Point(-1, -2);
this.rtb_Show.Name = "rtb_Show ";
this.rtb_Show.Size = new System.Drawing.Size(481, 356);
this.rtb_Show.TabIndex = 0;
this.rtb_Show.Text = " ";
//
// Knocker
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(477, 353);
this.Controls.Add(this.rtb_Show);
this.Name = "Knocker ";
this.Text = "Knocker ";
this.Load += new System.EventHandler(this.Knocker_Load);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.RichTextBox rtb_Show;
}
}

读书人网 >asp.net

热点推荐