20 lines
381 B
Go
20 lines
381 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
// 检查指定的 IP 和端口是否可达
|
|
func test_port(ip string, port int, timeout time.Duration) bool {
|
|
address := fmt.Sprintf("%s:%d", ip, port)
|
|
conn, err := net.DialTimeout("tcp", address, timeout)
|
|
if err != nil {
|
|
fmt.Printf("无法连接到 %s: %v\n", address, err)
|
|
return false
|
|
}
|
|
defer conn.Close()
|
|
return true
|
|
}
|