如题,用Java实现,谢谢大家

解决方案 »

  1.   

    以前也想过这个问题,应该不是很简单,主要用于分布式计算吧,可以用for循环的方式,在同一个网段内挨个检测,不过比较慢,
     对于分布式计算,最好应该知道至少一台计算机的ip
      

  2.   

    我觉得 只能这样实现..   有一个前提. 一定要 jdk 1.6 以上..需要用到 Console 类..
              1: exec("arp -a");
              2: 获取控制台的输出. 
              3: getHostName() 
              获取此 IP 地址的主机名。
      

  3.   

    2楼的思路貌似有可行性。
    或者,使用JNI与操作系统API交互?
      

  4.   

    arp命令不错啊,不过不用非得jdk6
    Runtime.getRuntime().exec("arp -a"); 也行啊。
      

  5.   

    需要用到 Console类   jdk1.6新加的..
    要不然怎么获取 控制台的输出???
      

  6.   

    你可以看一下Socket和ServerSocket,InetAddress等利用这些就可以获得局域网内活动 的IP。
      

  7.   

    只不过老版本的有很多陷阱需要注意下。
    http://www.blogjava.net/westwin/archive/2005/07/29/8698.html?opt=adminimport java.util.*;
    import java.io.*;class StreamGobbler extends Thread {
    InputStream is;
    String type; StreamGobbler(InputStream is, String type) {
    this.is = is;
    this.type = type;
    } public void run() {
    try {
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line = null;
    while ((line = br.readLine()) != null)
    System.out.println(type + ">" + line);
    } catch (IOException ioe) {
    ioe.printStackTrace();
    }
    }
    }public class GoodWindowsExec {
    public static void test(String cmdLine) {
    try {
    String osName = System.getProperty("os.name");
    String[] cmd = new String[3];
    cmd[0] = "cmd.exe";
    cmd[1] = "/C";
    cmd[2] = cmdLine;
    Runtime rt = Runtime.getRuntime();
    System.out.println("Execing " + cmd[0] + " " + cmd[1] + " "
    + cmd[2]);
    Process proc = rt.exec(cmd);
    // any error message?
    StreamGobbler errorGobbler = new StreamGobbler(proc
    .getErrorStream(), "ERROR"); // any output?
    StreamGobbler outputGobbler = new StreamGobbler(proc
    .getInputStream(), "OUTPUT"); // kick them off
    errorGobbler.start();
    outputGobbler.start(); // any error???
    int exitVal = proc.waitFor();
    System.out.println("ExitValue: " + exitVal);
    } catch (Throwable t) {
    t.printStackTrace();
    }
    } public static void main(String args[]) {
    test("arp -a");
    }
    }
      

  8.   

    原来我自己转的那份被csdn搞的乱七八糟,今天重贴了下。
    http://blog.csdn.net/sunyujia/archive/2007/10/20/1834271.aspx原作者:http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
      

  9.   

    公司的jdk不能随便升级,我还在用jdk1.4呢。
      

  10.   

    arp -a好像出的是绑定Ip的主机,活动的主机还是没出来吧?
    还有,万一换成Linux下怎么用?
      

  11.   

    想必楼主已经是百度完后,才来着发贴的,我在论坛呆的时间也不断了,没有什么好方法,依靠jni还有戏,java版块的哥们会c++的少,建议去C++要代码,封装jni的过程在回java版。
      

  12.   

    个人认为,基本没戏。你用任何tcp/upd发送,对方的防火墙可以不接收,这样你收不到任何反馈数据。别的?我唯一想到的,就是更改自己的IP地址,如果没有发生冲突,就说明那个IP是空闲的。否则证明是在线使用的IP但具体怎么做?我不知道!
      

  13.   

    也许 ipconfig 命令可以用一下吧, linux是ifconfig 
      

  14.   

    Ipconfig/ifconfig应该只是看自己的Ip吧,我想要一个通用型的
      

  15.   

    一个一个ping吧. 用多线程,一会儿就PING完了.
    Runtime.getRuntime().exec("ping " + ip);
      

  16.   


    别人机器也可以禁止你Ping的我觉得这个题目前提应该是不考虑防火墙的情况,没有返回消息还真没办法。
      

  17.   


    import java.net.*;
    import java.io.*;public class ScanIntranet
    {
    static int ip1 = 192;
    static int ip2 = 168;
    static int ip3 = 0;
    static int ip4 = 0;
    static int ipCount = 64; public static void main(String [] args)
    {
    while (ip3 < 256 && ip4 < 256)
    {
    Thread scanner = new ScanThread(ip3,ip4);
    scanner.start();
    ip4 += ipCount;
    if (ip4 > 255)
    {
    ip3++;
    ip4 = 0;
    }
    }
    }
    }class ScanThread extends Thread
    {
    int ip3;
    int ip4; public ScanThread(int ip3,int ip4)
    {
    this.ip3 = ip3;
    this.ip4 = ip4;
    System.out.println(Thread.currentThread() + " scanning from " + ip3 + "." + ip4 + " to " + ip3 + "." + (ip4 + ScanIntranet.ipCount));
    } private boolean checkIP(String ip) throws Exception
    {
    Runtime runtime = Runtime.getRuntime();
    String cmd = "ping " + ip;
    Process proc = runtime.exec(cmd);
    BufferedReader theReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    String infor = theReader.readLine();
    boolean alive = false;
    while (infor != null)
    {
    //System.out.println(infor);
    if (infor.startsWith("Reply from"))
    {
    alive = true;
    break;
    }
    else if (infor.startsWith("Request timed out"))
    {
    break;
    }
    infor = theReader.readLine();
    }
    theReader.close();
    proc.destroy();
    return alive;
    }
    public void run()
    {
    FileWriter writer = null;
    try
    {
    writer = new FileWriter("ip_alive.txt");
    }
    catch (Exception e) {} for (int i = 0; i < ScanIntranet.ipCount; i++)
    {
    try
    {
    if (ip3 > 255 || ip3 < 0)
    {
    ip3 = 255;
    }
    if (ip4 > 255 || ip4 < 0)
    {
    ip4 = 255;
    }
    String ip = ScanIntranet.ip1 + "." + ScanIntranet.ip2 + "." + ip3 + "." + ip4;
    boolean alive = checkIP(ip);
    if (alive)
    {
    System.out.println(ip + " is alive ");
    writer.write(ip);
    writer.flush();
    }
    else
    {
    System.out.println(ip + " is dead ");
    }
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    ip4++;
    }
    try
    {
    writer.close();
    }
    catch (Exception e) {}
    }
    }
      

  18.   

    [code]
    package com.util;import java.net.InetAddress;public class IpTest implements Runnable{
    private static StringBuffer targetIp;
    public  void searchIp(){
    try {
    InetAddress is = InetAddress.getLocalHost();
    byte[] ip = is.getAddress();
    targetIp = new StringBuffer();
    for(int i=0; i<(ip.length-1); i++) {
    if(i > 0) {
    targetIp.append(".");
    }
    targetIp.append(ip[i]&0xff);
    }
    //int a = ip[3]&0xff;
    new Thread(this).start();

    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    @Override
    public void run() {
    String host = null;
    try {
    for(int s=1; s<254; s++) {
    host = targetIp.toString() + "." + s;
    InetAddress it = InetAddress.getByName(host);
    System.out.println(host);
    String str = it.getHostName();
    if(!str.equals(host)) {
    System.out.println(host + "/" + str);
    }
    }
    } catch(Exception e) {
    e.printStackTrace();
    }
    }

    public static void main(String[] args) {
    new IpTest().searchIp();
    }
    }[/code]
    这是我的想法,不过速度太慢了,请各位高手给解决下,谢谢
      

  19.   


    import java.net.*;
    import java.io.*;public class ScanIntranet
    {
    static int ip1 = 192;
    static int ip2 = 168;
    static int ip3 = 0;
    static int ip4 = 0;
    static int ipCount = 64; public static void main(String [] args)
    {
    while (ip3 < 256 && ip4 < 256)
    {
    Thread scanner = new ScanThread(ip3,ip4);
    scanner.start();
    ip4 += ipCount;
    if (ip4 > 255)
    {
    ip3++;
    ip4 = 0;
    }
    }
    }
    }class ScanThread extends Thread
    {
    int ip3;
    int ip4; public ScanThread(int ip3,int ip4)
    {
    this.ip3 = ip3;
    this.ip4 = ip4;
    System.out.println(Thread.currentThread() + " scanning from " + ip3 + "." + ip4 + " to " + ip3 + "." + (ip4 + ScanIntranet.ipCount));
    } private boolean checkIP(String ip) throws Exception
    {
    Runtime runtime = Runtime.getRuntime();
    String cmd = "ping " + ip;
    Process proc = runtime.exec(cmd);
    BufferedReader theReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    String infor = theReader.readLine();
    boolean alive = false;
    while (infor != null)
    {
    //System.out.println(infor);
    if (infor.startsWith("Reply from"))
    {
    alive = true;
    break;
    }
    else if (infor.startsWith("Request timed out"))
    {
    break;
    }
    infor = theReader.readLine();
    }
    theReader.close();
    proc.destroy();
    return alive;
    }
    public void run()
    {
    FileWriter writer = null;
    try
    {
    writer = new FileWriter("ip_alive.txt");
    }
    catch (Exception e) {} for (int i = 0; i < ScanIntranet.ipCount; i++)
    {
    try
    {
    if (ip3 > 255 || ip3 < 0)
    {
    ip3 = 255;
    }
    if (ip4 > 255 || ip4 < 0)
    {
    ip4 = 255;
    }
    String ip = ScanIntranet.ip1 + "." + ScanIntranet.ip2 + "." + ip3 + "." + ip4;
    boolean alive = checkIP(ip);
    if (alive)
    {
    System.out.println(ip + " is alive ");
    writer.write(ip);
    writer.flush();
    }
    else
    {
    System.out.println(ip + " is dead ");
    }
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    ip4++;
    }
    try
    {
    writer.close();
    }
    catch (Exception e) {}
    }
    }
      

  20.   

    package com.util;import java.net.InetAddress;public class IpTest implements Runnable{
    private static StringBuffer targetIp;
    public  void searchIp(){
    try {
    InetAddress is = InetAddress.getLocalHost();
    byte[] ip = is.getAddress();
    targetIp = new StringBuffer();
    for(int i=0; i<(ip.length-1); i++) {
    if(i > 0) {
    targetIp.append(".");
    }
    targetIp.append(ip[i]&0xff);
    }
    //int a = ip[3]&0xff;
    new Thread(this).start();

    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    @Override
    public void run() {
    String host = null;
    try {
    for(int s=1; s<254; s++) {
    host = targetIp.toString() + "." + s;
    InetAddress it = InetAddress.getByName(host);
    System.out.println(host);
    String str = it.getHostName();
    if(!str.equals(host)) {
    System.out.println(host + "/" + str);
    }
    }
    } catch(Exception e) {
    e.printStackTrace();
    }
    }

    public static void main(String[] args) {
    new IpTest().searchIp();
    }
    }
      

  21.   

    终级应用级版本import java.util.*;
    import java.io.*;
    import java.net.*;public class ScanIntranet
    {
    static int ip1 = 192;
    static int ip2 = 168;
    static int ip3 = 0;
    static int ip4 = 0;
    static int ipCount = 4;
    static FileWriter writer = null;
        static SortedSet<String> ips = new TreeSet<String>();
    public static void main(String [] args)
    {
    try
    {
    writer = new FileWriter("ip_alive.txt");
    }catch (Exception e) {} int ip3Max = 256;
    int ip4Max = 256;
    Vector<ScanThread> allThreads = new Vector<ScanThread>();
    while (ip3 < ip3Max && ip4 < ip4Max)
    {
    Thread scanner = new ScanThread(ip3,ip4);
    scanner.start();
    allThreads.add((ScanThread)scanner);
    ip4 += ipCount;
    if (ip4 > ip4Max)
    {
    ip3++;
    ip4 = 0;
    }
    }
    while (true)
    {
    boolean allRun = false;
    for (ScanThread t : allThreads)
    {
    if (t.threadRun != false)
    {
    allRun = true;
    break;
    }
    }
    try
    {
    Thread.sleep(1000);
    } catch (Exception e) {}
    if (!allRun)
    {
    System.out.println("all thread scan over... ");
    try
    {
    Iterator<String> it = ips.iterator();
    System.out.println(ips.size() + " ips are alive");
    while (it.hasNext())
    {
    String ip = it.next();
    //System.out.println(ip);
    ip = ip.replaceAll("#","");
    writer.write(ip + "\r\n");
    }
    writer.close();
    } catch (Exception e) {}
    break;
    }
    }
    }
    }class ScanThread extends Thread
    {
    int ip3;
    int ip4; public boolean threadRun; public ScanThread(int ip3,int ip4)
    {
    this.ip3 = ip3;
    this.ip4 = ip4;
    System.out.println(this + " scanning from " + ip3 + "." + ip4 + " to " + ip3 + "." + (ip4 + ScanIntranet.ipCount - 1)); } private boolean checkIP(String ip) throws Exception
    {
    Runtime runtime = Runtime.getRuntime();
    String cmd = "ping " + ip;
    Process proc = runtime.exec(cmd);
    BufferedReader theReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
    String infor = theReader.readLine();
    boolean alive = false;
    while (infor != null)
    {
    //System.out.println(infor);
    if (infor.startsWith("Reply from"))
    {
    alive = true;
    break;
    }
    else if (infor.startsWith("Request timed out"))
    {
    break;
    }
    infor = theReader.readLine();
    }
    theReader.close();
    proc.destroy();
    return alive;
    }
    public void run()
    {
    threadRun = true;
    for (int i = 0; i < ScanIntranet.ipCount; i++)
    {
    try
    {
    if (ip3 > 255 || ip3 < 0)
    {
    ip3 = 255;
    }
    if (ip4 > 255 || ip4 < 0)
    {
    ip4 = 255;
    }
    String ip = ScanIntranet.ip1 + "." + ScanIntranet.ip2 + "." + ip3 + "." + ip4;
    boolean alive = checkIP(ip);
    if (alive)
    {
    System.out.println(ip + " is alive ");
    String ip3str = "";
    String ip4str = "";
    if (ip3 < 100)
    {
    ip3str = "#";
    if (ip3 < 10)
    {
    ip3str = "##";
    }
    }
    ip3str += ip3;
    if (ip4 < 100)
    {
    ip4str = "#";
    if (ip4 < 10)
    {
    ip4str = "##";
    }
    }
    ip4str += ip4; String hostName = InetAddress.getByName(ip).getCanonicalHostName();
    ip = ScanIntranet.ip1 + "." + ScanIntranet.ip2 + "." + ip3str + "." + ip4str + "\t\t" + hostName;
    ScanIntranet.ips.add(ip);
    }
    else
    {
    //System.out.println(ip + " is dead ");
    }
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    ip4++;
    }
    threadRun = false;
    System.out.println(this + " scann over... ");
    }
    }
      

  22.   

    pauliuyou 大侠,你在26楼发表的代码我在Vista下进行了测试,由于Vista下的CMD是完全中文的,所以无法正确的得到活动主机的列表。能有什么其他的解决方案吗?