php Xiaobian Yuzai may encounter the error message "UDP dial-up failed: Address has been used" when using the UDP dial-up function. This error is usually caused by the network port being occupied by another application. There are many ways to solve this problem. You can find out the applications occupying the port by viewing the list of currently running programs and close them; you can also try to modify the port settings of the dialer to avoid conflicts with already occupied ports. In short, as long as appropriate measures are taken according to the specific situation, this problem can be solved and UDP dial-up can be successfully completed.
I am writing a program to get a udp server. It works when I first get the server.
But when I enter the program for the second time. This gave me this error
panic: Listen failed:dial udp :8829->:9781: bind: address already in use
Code:
package main import ( "fmt" "log" "net" "os" ) func main() { log.SetFlags(log.Lshortfile) udpServer, err := net.ResolveUDPAddr("udp", ":9781") if err != nil { panic(fmt.Sprint("ResolveUDPAddr failed:", err.Error())) os.Exit(1) } client, err := net.ResolveUDPAddr("udp", ":8829") if err != nil { panic(err) } for { fmt.Printf(">> ") var input string fmt.Scanf("%s", &input) conn, err := net.DialUDP("udp", client, udpServer) if err != nil { panic(fmt.Sprint("Listen failed:", err.Error())) os.Exit(1) } _, err = conn.Write([]byte(input)) if err != nil { panic(fmt.Sprint("Write data failed:", err.Error())) os.Exit(1) } received := make([]byte, 1024) _, err = conn.Read(received) if err != nil { panic(fmt.Sprint("Read data failed:", err.Error())) os.Exit(1) } fmt.Printf("Response: %s\n", string(received)) } }
I think this is happening because I'm using the client twice.
But why does this result in "Address already in use"
I will not recreate the client.
Solutions I tried
One solution can be to set client to nil.
Set to nil to generate a random port.
But in my case, I need fixed port and fixed network address.
So this is not a solution.
Just create the connection before looping:
package main import ( "fmt" "log" "net" "os" ) func main() { log.SetFlags(log.Lshortfile) udpServer, err := net.ResolveUDPAddr("udp", ":9781") if err != nil { panic(fmt.Sprint("ResolveUDPAddr failed:", err.Error())) os.Exit(1) } client, err := net.ResolveUDPAddr("udp", ":8829") if err != nil { panic(err) } conn, err := net.DialUDP("udp", client, udpServer) if err != nil { panic(fmt.Sprint("Listen failed:", err.Error())) os.Exit(1) } for { fmt.Printf(">> ") var input string fmt.Scanf("%s", &input) _, err = conn.Write([]byte(input)) if err != nil { panic(fmt.Sprint("Write data failed:", err.Error())) os.Exit(1) } received := make([]byte, 1024) _, err = conn.Read(received) if err != nil { panic(fmt.Sprint("Read data failed:", err.Error())) os.Exit(1) } fmt.Printf("Response: %s\n", string(received)) } }
The above is the detailed content of UDP dialup failed: address already in use. For more information, please follow other related articles on the PHP Chinese website!