Commit 20a5e9cf authored by szh's avatar szh

优化获取本地ip流程

parent 2b41b1a9
...@@ -26,10 +26,11 @@ func Broadcast() { ...@@ -26,10 +26,11 @@ func Broadcast() {
panic("no serial no") panic("no serial no")
} }
} }
ip, err := GetRightIP() ips, err := GetRightIPs()
if err != nil { if err != nil {
panic(err) panic(err)
} }
ip := ips[0]
// 这里设置发送者的IP地址,自己查看一下自己的IP自行设定 // 这里设置发送者的IP地址,自己查看一下自己的IP自行设定
laddr := net.UDPAddr{ laddr := net.UDPAddr{
...@@ -49,10 +50,11 @@ func Broadcast() { ...@@ -49,10 +50,11 @@ func Broadcast() {
} }
for { for {
//ip 会变化 //ip 会变化
ip, err = GetRightIP() ips, err = GetRightIPs()
if err != nil { if err != nil {
panic(err) panic(err)
} }
ip = ips[0]
if runningAddr != nil && runningAddr.IP.String() != ip.IP.String() { if runningAddr != nil && runningAddr.IP.String() != ip.IP.String() {
laddr = net.UDPAddr{ laddr = net.UDPAddr{
...@@ -108,7 +110,7 @@ func GetRightIP() (ipv4 *net.IPNet, err error) { ...@@ -108,7 +110,7 @@ func GetRightIP() (ipv4 *net.IPNet, err error) {
return ip, err return ip, err
} }
func GetRightIps() (ipv4s []*net.IPNet, err error) { func GetRightIPs() (ipv4s []*net.IPNet, err error) {
ips := make([]*net.IPNet, 0) ips := make([]*net.IPNet, 0)
interfaces, err := net.Interfaces() interfaces, err := net.Interfaces()
if err != nil { if err != nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment