采用并行机制了!用多线程吧!
至于想预先判断IP是否存在,就只好先ping一下了!

解决方案 »

  1.   

    使用254 条并行线程各线程只要用socket 连接 139 端口(通则存在),比解析主机名快
      

  2.   

    using  System;  
    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;  
                                       }  
                           }  
               }  
     
    }
      

  3.   

    你怎么连续这么多问题?搞的好象要写一个pcanywhere 一样我上面说的:使用254 条并行线程
    各线程只要用socket 连接 139 端口(通则存在),比解析主机名快这个难道你不会?留言给我又要我帮你写程序吗?晕。。
      

  4.   

    这是我以前写的一个简易端口扫描,你改改用吧,为了节省空间我将所有控制定义去掉了,你瞧一下程序部分吧using System;
    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;
    }
    }