package test;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* 多线程设置超时 测试端口是否打开
* @author Administrator
*
*/
public class NetTest implements Runnable{
private Socket socket;
private int index;
public static int num=5000;
private String ip;
private int port;
private static Queue<String> allIp;
public NetTest(int n){
index=n;
}
public static void main(String[] args) {
allIp = new LinkedList<String>();
allIp.offer("172.16.2.120:3306");
allIp.offer("172.16.2.9:60");
allIp.offer("172.16.2.2:80");
allIp.offer("172.16.2.110:80");
allIp.offer("172.16.2.3:80");
System.out.println(allIp.size());
for(int i=0;i<allIp.size()+1;i++){
new Thread(new NetTest(i)).start();
}
}
@Override
public void run() {
try {
if((ip=getIP())!=null){
// Socket socket = new Socket(ip,port);
Socket socket = new Socket();
SocketAddress address = new InetSocketAddress(ip, port);
socket.connect(address, 3000); //设置超时
System.out.println(ip+"端口已开放");
}
} catch (IOException e) {
System.out.println(ip+"端口未开放");
}finally{
try {
if(socket!=null)
socket.close();
} catch (IOException e) {
}
}
}
public String getIP(){
synchronized (allIp) {
ip = allIp.poll();
String[] ips=ip.split(":");
ip=ips[0];
port=Integer.parseInt(ips[1]);
}
return ip;
}
}
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* 多线程设置超时 测试端口是否打开
* @author Administrator
*
*/
public class NetTest implements Runnable{
private Socket socket;
private int index;
public static int num=5000;
private String ip;
private int port;
private static Queue<String> allIp;
public NetTest(int n){
index=n;
}
public static void main(String[] args) {
allIp = new LinkedList<String>();
allIp.offer("172.16.2.120:3306");
allIp.offer("172.16.2.9:60");
allIp.offer("172.16.2.2:80");
allIp.offer("172.16.2.110:80");
allIp.offer("172.16.2.3:80");
System.out.println(allIp.size());
for(int i=0;i<allIp.size()+1;i++){
new Thread(new NetTest(i)).start();
}
}
@Override
public void run() {
try {
if((ip=getIP())!=null){
// Socket socket = new Socket(ip,port);
Socket socket = new Socket();
SocketAddress address = new InetSocketAddress(ip, port);
socket.connect(address, 3000); //设置超时
System.out.println(ip+"端口已开放");
}
} catch (IOException e) {
System.out.println(ip+"端口未开放");
}finally{
try {
if(socket!=null)
socket.close();
} catch (IOException e) {
}
}
}
public String getIP(){
synchronized (allIp) {
ip = allIp.poll();
String[] ips=ip.split(":");
ip=ips[0];
port=Integer.parseInt(ips[1]);
}
return ip;
}
}