采用并行机制了!用多线程吧!
至于想预先判断IP是否存在,就只好先ping一下了!
至于想预先判断IP是否存在,就只好先ping一下了!
解决方案 »
- 我想调用一个用dreamweaver做的动态图片的单击事件,要怎样实现?
- 关于try catch的问题了。
- 各位高手请看:
- .net中导出cvs文件如何处理特殊字符
- 怎样捕获C#控件(如Form,Panel)上的鼠标滚轮滚动事件?
- C#怎么打造WINDOWS优化大师的界面
- 关于C#廉洁ACCESS数据库后 使用UPDATE更新数据库报错的问题!
- c#修改打印机的尺寸
- 如何在C#中判断一个字符串时数字还是字符还是中文??在线高分求助!!!
- 请高手帮忙解决用Response.AddHeader下载时,我点击打开,要打开两次的问题,谢谢!
- webbrowser的bug,再加100分
- 各位大哥,帮帮我!!!
using System.DirectoryServices;
using System.Net;
class TestClass
{
static void Main (string[] args)
{
ShowComputers();
}
public static void ShowComputers()
{
//or use "WinNT://your_domain_name"
DirectoryEntry root = new DirectoryEntry("WinNT:"); DirectoryEntries domains = root.Children;
domains.SchemaFilter.Add("domain");
foreach (DirectoryEntry domain in domains)
{
Console.WriteLine(domain.Name);
DirectoryEntries computers = domain.Children;
computers.SchemaFilter.Add("computer");
foreach (DirectoryEntry computer in computers)
{
Console.WriteLine("\t" + computer.Name);
IPHostEntry iphe = Dns.GetHostByName(computer.Name);
foreach (IPAddress ip in iphe.AddressList)
{
Console.WriteLine("\t\t" + ip);
}
DirectoryEntries users = computer.Children;
}
}
}
}
各线程只要用socket 连接 139 端口(通则存在),比解析主机名快这个难道你不会?留言给我又要我帮你写程序吗?晕。。
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;using System.Threading;
using System.Net;
using System.Net.Sockets;
[STAThread]
static void Main()
{
Application.Run(new fm_Main());
} private void IPBox(){
ArLi.CommonPrj.MsgBox.Warning(this,"Invalid IP Format\nIP Format:\n\nNumber.Number.Number.*");
} public ArrayList threadList = new ArrayList(); private void GoScan(string ipAddr,int ServerPort) {
Poc clsPoc = new Poc();
clsPoc.myIPAddr = ipAddr;
clsPoc.ServerPort = ServerPort;
clsPoc.sForm = this;
ThreadStart tsTmp = new ThreadStart(clsPoc.PocScanOf);
Thread tdTmp = new Thread(tsTmp); lock (this.lstBox_Job) {
this.lstBox_Job.Items.Add(ipAddr);
this.stBar_Job.Text = this.lstBox_Job.Items.Count.ToString();
}
lock (this.threadList) {
this.threadList.Add(tdTmp);
} Application.DoEvents();
tdTmp.Start();
} private void btn_Start_Click(object sender, System.EventArgs e) {
this.threadList = new ArrayList();
this.lstBox_Job.Items.Clear();
this.lstBox_ok.Items.Clear();
this.txt_Port.Text = this.txt_Port.Text.Trim();
this.txt_IP.Text = this.txt_IP.Text.Trim(); if (this.txt_IP.Text == "" || this.txt_Port.Text == "") {
ArLi.CommonPrj.MsgBox.Warning(this,"IP or Port Empty!");
return;
} try {
int tmp = Int32.Parse(this.txt_Port.Text);
}
catch {
ArLi.CommonPrj.MsgBox.Warning(this,"Port Must Number");
} string[] myIP = this.txt_IP.Text.Trim().Split('.'); if (myIP.Length<4){
this.IPBox();
return;
} try {
int tmp;
for (int i=0; i<=2; i++) {
tmp = Int32.Parse(myIP[i]);
if (tmp <0 || tmp >254){
this.IPBox();
return;
}
}
}
catch {
this.IPBox();
return;
} try {
int tmp = Int32.Parse(myIP[3]);
this.gBox_Target.Enabled = false;
this.GoScan(String.Join(".",myIP),Int32.Parse(this.txt_Port.Text.Trim()));
}
catch {
if (myIP[3] == "*") {
this.gBox_Target.Enabled = false;
for (int i=1; i<=254; i++) {
this.GoScan(myIP[0] + "." + myIP[1] + "." + myIP[2] + "." + i.ToString(),Int32.Parse(this.txt_Port.Text.Trim()));
}
}
else {
this.IPBox();
return;
}
}
} private void fm_Main_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
e.Cancel = ! this.gBox_Target.Enabled;
if (! this.gBox_Target.Enabled) {
ArLi.CommonPrj.MsgBox.Warning(this,"wait Socket connect Close");
}
} private void gBox_Target_EnabledChanged(object sender, System.EventArgs e) {
this.picBox_Wait.Visible = (! this.gBox_Target.Enabled);
} private void fm_Main_Load(object sender, System.EventArgs e) {
this.Text = Application.ProductName;
ArLi.CommonPrj.getResPrj ObjTmp = new ArLi.CommonPrj.getResPrj();
this.Icon = (Icon)ObjTmp.GetResOf("PortScanEx.Image","ico_Main");
this.picBox_Wait.Image = (Image)ObjTmp.GetResOf("PortScanEx.Image","wiz");
// this.picBox_Wait.Location = this.gBox_Target.Location;
// this.picBox_Wait.Size = this.gBox_Target.Size;
} private void lstBox_ok_SelectedIndexChanged(object sender, System.EventArgs e) {
this.cMnu_Ok_Copy.Enabled = (this.lstBox_ok.SelectedItem.ToString() != "");
} private void cMnu_Ok_Copy_IP_Click(object sender, System.EventArgs e) {
Clipboard.SetDataObject(this.lstBox_ok.SelectedItem.ToString(),true);
} private void cMnu_Ok_Copy_IP_Port_Click(object sender, System.EventArgs e) {
Clipboard.SetDataObject(this.lstBox_ok.SelectedItem.ToString() + ":" + this.txt_Port.Text,true);
}
} public class Poc{
public string myIPAddr = "";
public int ServerPort = 0;
public fm_Main sForm = null; public void PocScanOf() {
IPAddress ipAddress = IPAddress.Parse(this.myIPAddr);
IPEndPoint HostEP = new IPEndPoint(ipAddress,this.ServerPort); Socket sock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
sock.SetSocketOption(
SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout,
60 * 1000
);
sock.SetSocketOption(
SocketOptionLevel.Socket,
SocketOptionName.SendTimeout,
60 * 1000
); try {
sock.Connect(HostEP);
byte[] bytesSendStr = new byte[1]{0};
sock.Send(bytesSendStr,bytesSendStr.Length,0);
lock (sForm.lstBox_ok) {
sForm.lstBox_ok.Items.Add(this.myIPAddr);
sForm.stBar_Ok.Text = sForm.lstBox_ok.Items.Count.ToString();
}
}
catch (Exception e){
sForm.stBar_Stats.Text = e.Message;
}
finally {
lock (sForm.lstBox_Job) {
sForm.lstBox_Job.Items.Remove(this.myIPAddr);
sForm.stBar_Job.Text = sForm.lstBox_Job.Items.Count.ToString();
if (sForm.lstBox_Job.Items.Count == 0) {
sForm.gBox_Target.Enabled = true;
}
}
try {
sock.Shutdown(SocketShutdown.Both);
}catch{}
sock.Close();
sock = null;
}
}