tt/utils/tcp/port.go

25 lines
407 B
Go
Raw Normal View History

2024-05-28 08:47:31 +08:00
package tcp
import (
"fmt"
"net"
"time"
)
// CheckPortConnection 检测端口是否监听
func CheckPortConnection(ip string, port int) bool {
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), 3*time.Second)
if err != nil {
return false
} else {
if conn != nil {
defer func(conn net.Conn) {
_ = conn.Close()
}(conn)
return true
} else {
return false
}
}
}