admin
2021-08-07 2b53a01ef7275e3bd708529ed64042ff480a9f73
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package org.yeshi.utils;
 
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
 
public class NetWorkUtil {
 
    /**
     * 指定端口是否可连接
     * @param host
     * @param port
     * @param timeOut
     * @return
     */
    public static boolean isHostConnectable(String host, int port,int timeOut) {
        Socket socket = new Socket();
        try {
            socket.connect(new InetSocketAddress(host, port),timeOut);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }
 
}