libp2p / go-reuseport

reuse tcp/udp ports in golang
ISC License
763 stars 107 forks source link

[Need Help] testing udp receive and send in two debian PCs in my LAN #98

Closed diyism closed 1 year ago

diyism commented 1 year ago

the test.go:

package main

import (
    "fmt"
    "time"

    reuseport "github.com/libp2p/go-reuseport"
)

func main() {
    go func() {
    // Create a new UDP socket using the reuseport package
    conn, err := reuseport.ListenPacket("udp", "0.0.0.0:15443")
    if err != nil {
        fmt.Println("Error listening:", err)
        return
    }
    defer conn.Close()
    // Continuously read data from the UDP socket and print it to the console
    for {
        buf := make([]byte, 1024)
        n, addr, err := conn.ReadFrom(buf)
        if err != nil {
            fmt.Println("Error reading:", err)
            continue
        }

        fmt.Printf("Received %d bytes from %s: %s\n", n, addr.String(), string(buf[:n]))
    }
    }()

    go func() {
        // Create a new UDP socket using the reuseport package
        conn, err := reuseport.Dial("udp", "0.0.0.0:15443", "192.168.0.1:15443")
        if err != nil {
            fmt.Println("Error connecting:", err)
            return
        }
        defer conn.Close()

        for {
            msg := []byte("Hello, world!")
            _, err := conn.Write(msg)
            if err != nil {
                //fmt.Println("Error sending message:", err)
                continue
            }

            //fmt.Println("Message sent:", string(msg))
            time.Sleep(1 * time.Second) // Wait for 1 second before sending the next message
        }
    }()

    go func() {
        // Create a new UDP socket using the reuseport package
        conn, err := reuseport.Dial("udp", "0.0.0.0:15443", "192.168.0.2:15443")
        if err != nil {
            fmt.Println("Error connecting:", err)
            return
        }
        defer conn.Close()

        for {
            msg := []byte("Hello, world!")
            _, err := conn.Write(msg)
            if err != nil {
                //fmt.Println("Error sending message:", err)
                continue
            }

            //fmt.Println("Message sent:", string(msg))
            time.Sleep(1 * time.Second) // Wait for 1 second before sending the next message
        }
    }()
    time.Sleep(100000 * time.Second)
}

compile the test.go and put it into the 192.168.0.1 and 192.168.0.2, and exec it in the two machines, but there's no message printed in both of the two machines, but if i stop one of them and keep the other run, and run "nc -u -p 15443 192.168.0.1 15443" in the stopped one, I can see many "Hello, world!" printing.

I can't understand why, any hint?

diyism commented 1 year ago

Asked openai chatgpt, it tell me the right answer, get rid of conn, err := reuseport.Dial("udp", "0.0.0.0:15443", "192.168.0.2:15443") and replace ", err := conn.Write(msg)" with ", err := conn.WriteTo(msg, &net.UDPAddr{IP: net.ParseIP("192.168.0.2"), Port: 15443})" great, now it works, I close this issue.