package cn.webank.welb.admin.common.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
/**
* Util for testing target instance tcp connection
*/
public class NetUtil {
private static Logger logger = LoggerFactory.getLogger(NetUtil.class);
public static boolean isHostCanConnect(String host, int port) {
Socket socket = new Socket();
try {
int timeoutMills = 1000;
SocketAddress endpoint = new InetSocketAddress(host, port);
socket.connect(endpoint, timeoutMills);
} catch (IOException e) {
return false;
} finally {
try {
socket.close();
} catch (IOException e) {
}
}
return true;
}
public static boolean isHostCanConnect(String host, int port, int timeoutSeconds) {
Socket socket = new Socket();
try {
SocketAddress endpoint = new InetSocketAddress(host, port);
socket.connect(endpoint, timeoutSeconds);
} catch (IOException e) {
return false;
} finally {
try {
socket.close();
} catch (IOException e) {
}
}
return true;
}
}
检验tpc连接
最新推荐文章于 2023-08-29 23:53:27 发布