use listen to ensure the port is free (#6990)

This commit is contained in:
boxjan 2021-07-05 06:24:18 +08:00 committed by GitHub
parent a21d28d01b
commit 0c685c1e94
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -29,12 +29,12 @@ func IsIPV6(ip _net.IP) bool {
// IsPortAvailable checks if a TCP port is available or not // IsPortAvailable checks if a TCP port is available or not
func IsPortAvailable(p int) bool { func IsPortAvailable(p int) bool {
conn, err := _net.Dial("tcp", fmt.Sprintf(":%v", p)) ln, err := _net.Listen("tcp", fmt.Sprintf(":%v", p))
if err != nil { if err != nil {
return true return false
} }
defer conn.Close() defer ln.Close()
return false return true
} }
// IsIPv6Enabled checks if IPV6 is enabled or not and we have // IsIPv6Enabled checks if IPV6 is enabled or not and we have