只是一个小的socket程序,查看本机上目前哪些端口在用,代码如下:
import java.net.*;
public class LocalScan{
public static void main(String[] args){
for(int i=1;i<1024;i++)
{
testPort(i);
}
System.out.println("Completed");
}
private static void testPort(int i){
try{
ServerSocket sock=new ServerSocket(i);
}
catch (java.io.IOException e){
System.out.println("Port " + i + " in use.");
}
}
}
我用的是JDK1.6.0,在win2000下运行结果是:
Port 135 in use.
Port 23 in use.
Port 445 in use.
而在win-xp环境下,除了上述3个端口外,从端口446开始一直到端口1023都显示正在使用!(i最大取值为1023)
这是为什么呢??系统处理结果不一样,还是程序有漏洞??还有在xp下编译源代码时会提示有错,但直接运行时正常
洗耳恭听!
import java.net.*;
public class LocalScan{
public static void main(String[] args){
for(int i=1;i<1024;i++)
{
testPort(i);
}
System.out.println("Completed");
}
private static void testPort(int i){
try{
ServerSocket sock=new ServerSocket(i);
}
catch (java.io.IOException e){
System.out.println("Port " + i + " in use.");
}
}
}
我用的是JDK1.6.0,在win2000下运行结果是:
Port 135 in use.
Port 23 in use.
Port 445 in use.
而在win-xp环境下,除了上述3个端口外,从端口446开始一直到端口1023都显示正在使用!(i最大取值为1023)
这是为什么呢??系统处理结果不一样,还是程序有漏洞??还有在xp下编译源代码时会提示有错,但直接运行时正常
洗耳恭听!
1)程序基本上可行
2)程序不能测试UDP端口是否已用
3)建议程序testPort()部分修改如下:private static void testPort(int i){
try{
Socket s=new Socket();
s.bind(new InetSocketAddress(InetAddress.getLocalHost(),i));
s.close();
DatagramSocket ds=new DatagramSocket(i,InetAddress.getLocalHost());
ds.close();
}
catch (java.lang.Exception e){
System.out.println("Port " + i + " in use.");
}
}
答:我怀疑是你的原来的代码中:
try{
ServerSocket sock=new ServerSocket(i);
sock.close();//加上这句可能就好了.
}
可能的理由是:同时打开处于ServerSocket 监听状态的 个数是有限制的(操作系统内部).故要及时close().