Skip to content

Commit

Permalink
pref: 把监听UDP的for代码段包进闭包函数里
Browse files Browse the repository at this point in the history
  • Loading branch information
taills committed Jan 10, 2024
1 parent 1e6c921 commit 3e540b8
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions replicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,19 @@ func (tr *TrafficReplicator) Stop() {
// None
func (tr *TrafficReplicator) runUDP() {
for _, port := range tr.Ports {
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", port))
if err != nil {
fmt.Println("ResolveUDPAddr failed:", err)
continue
}

conn, err := net.ListenUDP("udp", addr)
if err != nil {
fmt.Println("ListenUDP failed:", err)
continue
}

go tr.handleUDPConnection(conn, port)
go func(port int) {
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", port))
if err != nil {
fmt.Println("ResolveUDPAddr failed:", err)
return
}
conn, err := net.ListenUDP("udp", addr)
if err != nil {
fmt.Println("ListenUDP failed:", err)
return
}
go tr.handleUDPConnection(conn, port)
}(port)
}
}

Expand Down

0 comments on commit 3e540b8

Please sign in to comment.