比如cs的查找服务器一下,查找局域网内开启某一端口的所有服务器IP地址。
解决方案 »
- 请问在Java中,在接口内部定义类的意义或者作用是什么
- jsp初学者所遇的问题
- actionAdapter 出错......
- 问高手,哪位用到Response.ContentType = 'application/pdf';这方面的知识了,请给我个例子,谢谢
- NetBean4.0中不能用"."后面会有method的提示的问题!
- 想看一Java的代码,去哪个网站好呢?
- 请问:如何实现splash screen?
- 这几行代码为什么可以由域名得到IP,但由IP得不到域名?麻烦大侠帮看看,谢谢先!
- 请问在java里怎么读用c写在文件里的数据结构,来着给份
- MouseEvent.BUTTON3_MASK 是什么意思?
- 匿名内部类访问其外部类的方法的问题
- 运行CMD命令行变慢
http://wenda.tianya.cn/wenda/thread?tid=5348550a27e453ba
http://topic.csdn.net/t/20030711/15/2017115.html
private InetAddress IP;
private int port;
private int timesDropped = 0;
private int timesClosed = 0;
private static int SOCKET_TIMEOUT = 2000;
private static int MAX_TIMES_DROPPED = 3;
private static int MAX_TIMES_CLOSED = 2; /*
*Constructs new thread to Scan host on TCP port
*@param InetAddress the IP address of host to scan
*@param int port number to scan
*/
public ScanTCP(InetAddress IP, int port){
this.IP = IP;
this.port = port; // if we are running on Windows, increase timeout and no. of retries
if (System.getProperty("os.name").startsWith("Windows")){
this.SOCKET_TIMEOUT = 2500;
this.MAX_TIMES_DROPPED = 4;
this.MAX_TIMES_CLOSED = 3;
}
} /**
*Returns the IP address being scanned
*/
public InetAddress getIP(){
return IP;
} /**
*Return the port being scanned
*/
public int getPort(){
return port;
} /**
*Scans host/port using the TCP protocol
*/
public void run() {
try{
String portsStatus = this.scanTCP();
setChanged();
notifyObservers(portsStatus);
}
catch(NoRouteToHostException e){
setChanged();
notifyObservers("null"); //null to signify error contacting host
return;
}
} /**
*Scans single port using specified TCP hoost/port
*@return String - either OPEN CLOSED DROPPED
*/
public String scanTCP() throws NoRouteToHostException{
try{
Socket s = new Socket(); //set the socket to timeout, multiply the timeout by number of retry
s.connect(new InetSocketAddress(IP, port),
this.SOCKET_TIMEOUT * ((timesDropped | timesClosed)+1));
s.close();
}
catch(NoRouteToHostException e){
throw e; //throw to calling
}
catch(SocketTimeoutException e){
/*
* Sometimes a socket times out in error (due to intensive scanning)
*check again, to make sure
*/
if (this.timesDropped < this.MAX_TIMES_DROPPED){
this.timesDropped++;
try{
Thread.sleep(2500); // wait before trying again
}
catch(Exception f){
}
// scan again!
return this.scanTCP();
}
else{
return "DROPPED";
}
}
catch(IOException e){
if (this.timesClosed < this.MAX_TIMES_CLOSED){
this.timesClosed++;
try{
Thread.sleep(2500);
}
catch(Exception f){
}
return this.scanTCP();
}
else{
return "CLOSED";
}
}
//if got this far, is opem
return "OPEN";
}}