java中
只知道本电脑的ip或电脑名如何通过本机知道局域网的其他电脑
的Ip的??

解决方案 »

  1.   

    用循环,把局域网中的ip遍历,如果返回东西,就证明这个ip存在。好像要用到tcp和socket,好早用过,忘记了,不过速度真的很慢。
      

  2.   


    //最笨的方法:用多个线程ping每一台主机
    import java.io.*;
    import java.util.*;
    public class MyScanner{
       
       public MyScanner(){
          
       }
       
       public static int count=0;
       
       public static void main(String args[]) throws Exception{       MyScanner myScanner=new MyScanner();
        
           System.out.println("...start to scan....");
           ArrayList<Thread> list=new ArrayList<Thread>();
           for(int i=0;i<256;i+=5){
              Thread t=new Thread(new ScanThread(i));
              t.start();
              list.add(t);
           }
           for(int i=0;i<list.size();i++){
              try{
                 list.get(i).join();
              }catch(Exception e){ e.printStackTrace();}
          }
          System.out.println("...end to scan....");
          System.out.println("count="+count);  
          }
    }
    class ScanThread extends Thread{
            public int startIp;
            public ScanThread(int startIp){
                 this.startIp=startIp;
            }
            public void run(){
            Runtime runtime=Runtime.getRuntime();
            for(int i=startIp;i<startIp+5;i++){
             String ipStr="192.168.132."+i;
          String cmd="ping "+ipStr;
           try{
          Process proc = runtime.exec(cmd);
          BufferedReader theReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
          String infor ="";
          while((infor=theReader.readLine())!=null){
           if (infor.startsWith("Reply from")){
               MyScanner.count++;
            System.out.println(ipStr+" is alive!");
            break;
           } else if (infor.startsWith("Request timed out")){
            //System.out.println(ipStr+" is dead!");
            break;
           }
          }
          theReader.close();
          proc.destroy();
          }catch(Exception e){e.printStackTrace();}
        }
      } 
       }
      

  3.   


    奇怪了,为什么用了楼上的方法,所有的IP都有主机?可实际上,局域网中只有2台主机啊...start to scan....
    192.168.1.0 is alive!
    192.168.1.1 is alive!
    192.168.1.75 is alive!
    192.168.1.50 is alive!
    192.168.1.130 is alive!
    192.168.1.35 is alive!
    192.168.1.20 is alive!
    192.168.1.55 is alive!
    192.168.1.5 is alive!
    192.168.1.45 is alive!
    192.168.1.60 is alive!
    192.168.1.65 is alive!
    192.168.1.25 is alive!
    192.168.1.15 is alive!
    192.168.1.40 is alive!
    192.168.1.106 is alive!
    192.168.1.30 is alive!
    192.168.1.115 is alive!
    192.168.1.185 is alive!
    192.168.1.80 is alive!
    192.168.1.120 is alive!
    192.168.1.110 is alive!
    192.168.1.170 is alive!
    192.168.1.145 is alive!
    192.168.1.245 is alive!
    192.168.1.155 is alive!
    192.168.1.90 is alive!
    .
    .
    .
    ...end to scan....
    count=253
      

  4.   

    ipconfig看自己的net view到其他人的主机名,然后挨着ping
      

  5.   

    如果知道自己的ip
    用循环的又怎么知道ip的起始点和结束点?
      

  6.   

    现在 ping  一般是 ping不到的!稍微有个防火墙!360什么的就给你屏蔽掉!!等待高手给更好的方法
      

  7.   

    给点意见
    记得TCP协议的握手 
    向局域网广播TCP数据包 看谁抛出异常 抛出异常的都是没开机的 或没有的唉不太懂 看看大牛的