1 Star 0 Fork 0

Uni-Minds/utils

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
network.go 609 Bytes
Copy Edit Raw Blame History
Liuxy authored 2022-10-15 14:14 . Release 1.0.0
package tools
import (
"fmt"
"net"
"time"
)
func IsPortInUse(port int) bool {
if conn, err := net.DialTimeout("tcp", net.JoinHostPort("", fmt.Sprintf("%d", port)), 3*time.Second); err == nil {
conn.Close()
return true
}
return false
}
func LocalIP() string {
ip := ""
if addrs, err := net.InterfaceAddrs(); err == nil {
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && !ipnet.IP.IsMulticast() && !ipnet.IP.IsLinkLocalUnicast() && !ipnet.IP.IsLinkLocalMulticast() && ipnet.IP.To4() != nil {
ip = ipnet.IP.String()
}
}
}
return ip
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/uni-minds/utils.git
git@gitee.com:uni-minds/utils.git
uni-minds
utils
utils
v1.0.2

Search