Home > Backend Development > Golang > UDP dialup failed: address already in use

UDP dialup failed: address already in use

PHPz
Release: 2024-02-09 21:27:11
forward
401 people have browsed it

UDP 拨号失败:地址已被使用

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.

Question content

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
Copy after login

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))
    }

}
Copy after login

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.

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))
    }

}
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template