怎么样扫描计算机系统本地和远程的端口,监测其是打开还是关闭的在很多应用程序中都要用到,下面是用java实现的简单的端口扫描程序。
source code:
--------------------------------------------------------------------------------
/*
* created on 2005-3-22
*
* todo to change the template for this generated file go to
* window - preferences - java - code style - code templates
*/
/**
* @author whandey connect to me: whandey@163.com
*
* todo to change the template for this generated type comment go to
* window - preferences - java - code style - code templates
*/
import java.io.*;
import java.net.*;
import java.util.*;
public class socketport {
public static void main(string[] args) {
string ip = "159.162.39.27";
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 ");
fout.println();
// do ports scan
for(int nport = 25;nport < 30;++nport)
{
try
{
socket s = new socket(hostname,nport);
fout.println("the port " + nport + " is open!");
fout.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!");
}
}
fout.close();
}
catch(ioexception e){}
}
}
闽公网安备 35060202000074号