Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

udp port 30620 #114

Open
97FBW opened this issue Jun 1, 2022 · 1 comment
Open

udp port 30620 #114

97FBW opened this issue Jun 1, 2022 · 1 comment

Comments

@97FBW
Copy link

97FBW commented Jun 1, 2022

Hello, I have looked at nacos-Go-SDK, in his SDK there is an extra handling of ports, the code is as follows:

@97FBW
Copy link
Author

97FBW commented Jun 1, 2022

func (us *PushReceiver) tryListen() (*net.UDPConn, bool) {
addr, err := net.ResolveUDPAddr("udp", us.host+":"+strconv.Itoa(us.port))
if err != nil {
logger.Errorf("can't resolve address,err: %+v", err)
return nil, false
}

conn, err := net.ListenUDP("udp", addr)
if err != nil {
	logger.Errorf("error listening %s:%d,err:%+v", us.host, us.port, err)
	return nil, false
}

return conn, true

}

func (us *PushReceiver) getConn() *net.UDPConn {
for i := 0; i < 3; i++ {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
port := r.Intn(1000) + 54951
us.port = port
conn, ok := us.tryListen()
if ok {
logger.Infof("udp server start, port: " + strconv.Itoa(port))
return conn
}
if !ok && i == 2 {
logger.Errorf("failed to start udp server after trying 3 times.")
}
}
return nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant