相信很多人都用过类似“飞鸽传书”的局域网内聊天工具吧??

打开的时候软件会对局域网内所有用户进行扫描,把在线的都列出来

我想,它应该就是一开软件就对局域网上所有用户的某个端口发送消息,如果有开这个软件,它就会回复自己的ip、计算机名等信息的。

但是我们学的java对一个端口发送消息等待回应,如果那台机端口关闭的话好象要等很久啊??

我想问的就是:
如何快速的对整个局域网的所有计算机的某个端口扫描,辨别那个端口是否打开??
网友回复:那是使用广播实现的。可不是每个机器进行尝试连接哦!

网友回复:用过飞鸽,没研究过....顶下
网友回复: 引用 1 楼 java2000_net 的回复:
那是使用广播实现的。可不是每个机器进行尝试连接哦!


我说呢,我一直郁闷为什么我来一下那么慢他来一下那么快呢

那么有没有谁有这样的小例子发来参考参考??
如果没有的话,也欢迎提供几个需要用到的类,我看看api doc也好,先谢谢咯
网友回复:建议看一下nmap ,一个开源的端口扫描工具.
网友回复:mark 学习
网友回复:
Java code
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
import java.io.*;
import java.net.*;
import java.util.*;
public class SocketPort {
public static void main(String[] args) {
 String ip = "192.168.0.1"; 
 String hostname = new String(); 
 try{ //get the target ip address and hostname
   InetAddress address = InetAddress.getByName(ip);
   System.out.println(address);
   hostname = address.getHostName();
   System.out.println(hostname);
 }
 catch(UnknownHostException e){
    System.out.println("Could not find "  ip);
 }
 try{ // creat the output file
    PrintWriter fout = new PrintWriter( new FileWriter("PortInf.txt"));
    fout.println("Information Of The Port On the "   hostname  "computer ");
    System.out.println("Information Of The Port On the "   hostname  "computer ");
    fout.println();
 // do ports scan
 for(int nport = 24;nport <= 60;  nport){
   try{
   Socket s = new Socket(hostname,nport);
   fout.println("The port "   nport   " is open!");
   System.out.println("The port "   nport   " is open!");
   fout.println("Connected to "  s.getInetAddress()   " on port "   s.getPort()   " from port "        s.getLocalPort()   " of "   s.getLocalAddress());
   System.out.println("Connected to "  s.getInetAddress()   " on port "   s.getPort()   " from port "         s.getLocalPort()   " of "   s.getLocalAddress());
  //print the connected socket information
}
catch(IOException e){
   fout.println("The port "   nport   " is closed!");
   System.out.println("The port "   nport   " is closed!");
}
}
fout.close();
}
catch(IOException e){}
}
}

网友回复:http://www.sudu.cn/info/html/edu/jsp/20071219/121327.html
网友回复:java.net.DatagramSocket 能用到这个类。
网友回复:找了一天,广播的没有找到,找到个所谓的多播 MulticastSocket
虽然还没实现功能,继续摸索

晚上回来结贴,有补充的继续补充哦,嘿嘿
网友回复:顶一下
网友回复:import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class IPTest {

public static void main(String[] args) {
String str = "";

try {
for (int i = 2; i < 254; i ) {

Process p = Runtime.getRuntime().exec(
"ping 192.168.0." i " -w 300 -n 1");
InputStreamReader is = new InputStreamReader(p.getInputStream());
LineNumberReader lnr = new LineNumberReader(is);

for (int j = 1; j < 7; j ) {
lnr.readLine();

}

str = lnr.readLine();
if (str.length() < 17
|| str.substring(8, 17).equals("timed out")) {
continue;
} else {
System.out.println("192.168.0." i);
}
}

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

}

扫描局域网内的IP地址,自己试试
网友回复:补充一句,上边那个示例曾经做过飞鸽,但没有完全实现
网友回复:不是你去问它,是它来告诉你!
网友回复:Broadcast
记得以前弄过局域网的即时通 好像用到过·
网友回复:
Java code
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.PortUnreachableException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
public class ClientFinder {
    private static final int PORT = 6789;
    private static final String REQ_MSG = "HI";
    private static final String RES_MSG = "OK";
    private static byte[] REQ_DATA = null;
    private static byte[] RES_DATA = null;
    static {
        try {
            REQ_DATA = REQ_MSG.getBytes("UTF-8");
            RES_DATA = RES_MSG.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
    public static class Listener implements Runnable {
        private static final int BUF_SIZE = 100;
        private final DatagramSocket ds;
        public Listener(DatagramSocket ds) {
            if (ds == null) throw new IllegalArgumentException("ds is null");
            this.ds = ds;
        }
        public void run() {
            DatagramPacket dp = new DatagramPacket(new byte[BUF_SIZE], 0, BUF_SIZE);
            try {
                while (true) {
                    try {
                        this.ds.receive(dp);
                        InetAddress ipa = dp.getAddress();
                        String msg = new String(
                            dp.getData(),
                            dp.getOffset(),
                            dp.getLength(),
                            "UTF-8"
                        );
                        if (REQ_MSG.equals(msg)) {
                            this.ds.send(new DatagramPacket(
                                RES_DATA, 0, RES_DATA.length,
                                dp.getAddress(), dp.getPort()
                            ));
                        } else if (RES_MSG.equals(msg)) {
                            System.out.println(ipa   " > "   msg);
                        } else {
                        }
                    } catch (SocketTimeoutException e) {
                    } catch (PortUnreachableException e) {
                    } catch (SocketException e) {
                        throw e;
                    } catch (IOException e) {
                    }
                }
            } catch (SocketException e) {
            }
        }
    }
    public static class Sender implements Runnable {
        private final DatagramSocket ds;
        public Sender(DatagramSocket ds) {
            if (ds == null) throw new IllegalArgumentException("ds is null");
            this.ds = ds;
        }
        public void run() {
            try {
                this.ds.send(new DatagramPacket(
                    REQ_DATA, 0, REQ_DATA.length,
                    new InetSocketAddress("255.255.255.255", PORT)
                ));
            } catch (PortUnreachableException e) {
            } catch (SocketException e) {
            } catch (IOException e) {
            }
        }
    }
    public static void main(String[] args) {
        DatagramSocket dgskt = null;
        try {
            dgskt = new DatagramSocket(PORT);
            new Thread(new Listener(dgskt)).start();
            new Thread(new Sender(dgskt)).start();
        } catch (SocketException e) {
            e.printStackTrace();
        } finally {
            if (dgskt != null) {
                //dgskt.close();
            }
        }
    }
}