1
votes

As I'm finding quite some examples how to write a go server and client, this works locally on one machine.

Now I'm trying to communicate in my local network between two PCs, one running the go server script, one the client.

However, I can't establish a connection because of the error:

Error: listen udp 192.168.11.6:10001: bind: cannot assign requested address
panic: runtime error: invalid memory address or nil pointer dereference [signal 0xb code=0x1 addr=0x0 pc=0x401376] // ...

Of course I will post my code (client, where the problem occurs):

package main

import (
    "fmt"
    "net"
    "strconv"
    "time"
)

func CheckError(err error) {
    if err != nil {
        fmt.Println("Error: ", err)
    }
}

func main() {
    ServerAddr, err := net.ResolveUDPAddr("udp", "192.168.11.6:10001")
    CheckError(err)

    Conn, err := net.ListenUDP("udp", ServerAddr)
    CheckError(err)

    defer Conn.Close()
    i := 0
    for {
        msg := strconv.Itoa(i)
        i++
        buf := []byte(msg)
        _, err = Conn.WriteToUDP(buf, ServerAddr)
        time.Sleep(time.Second * 1)
    }
}

Server:

package main
import (
    "fmt"
    "net"
    "os"
)

/* A Simple function to verify error */
func CheckError(err error) {
    if err  != nil {
        fmt.Println("Error: " , err)
        os.Exit(0)
    }
}

func main() {
    ServerAddr,err := net.ResolveUDPAddr("udp",":10001")
    CheckError(err)

    ServerConn, err := net.ListenUDP("udp", ServerAddr)
    CheckError(err)
    defer ServerConn.Close()

    buf := make([]byte, 1024)
    for {
        fmt.Println("Starting...")
        n,addr,err := ServerConn.ReadFromUDP(buf)
        fmt.Println("Received ",string(buf[0:n]), " from ",addr)
        ServerConn.WriteToUDP([]byte("hello there!"), addr)
        if err != nil {
            fmt.Println("Error: ",err)
        }
    }
}

The client has the local network IP address 192.168.11.8 and the server 192.168.11.6. They can also ping each other, and I'm opening the port when Windows asks for it.

I'm happy about all suggestions. I struggle with this because I only find localhost server client go examples.

1
The client is trying to bind to the wrong address.JimB
Thanks for the reply @JimB - what address is the correct one? I thought I listen at the server's ip addressDaniel
Your client is trying to listen, as if it were the server.Michael Hampton
@MichaelHampton, there is no client or server in UDP, and you normally bind UDP using Listen regardless, as Dial creates a “connected” UDP socket that has slightly different behavior.JimB

1 Answers

1
votes

Okay, I figured it out. Weird that I had to allow the app to communicate on public networks? While I am on my home network. When starting the server - I had to enable public networks

I figured out that I do not have to open a socket on the client side, but use DialUDP

Also the server needed the full Ip address in
ServerAddr,err := net.ResolveUDPAddr("udp","192.168.11.6:10001")

Improved Client code: (main func)

ServerAddr, err := net.ResolveUDPAddr("udp", "192.168.11.6:10001")
CheckError(err)
buf := make([]byte, 1024)
Conn, err := net.DialUDP("udp", nil, ServerAddr)
CheckError(err)

defer Conn.Close()
i := 0
for {
    msg := strconv.Itoa(i)
    i++
    fmt.Printf(msg)
    n, err := Conn.Write([]byte(msg))
    CheckError(err)
    fmt.Printf("sent %d bytes", n)
    n, addr, err := Conn.ReadFromUDP(buf)
    if err == nil {
        fmt.Printf("%s %s\n", buf, addr)
    } else {
        fmt.Printf("some err %v\n", err)
    }
    time.Sleep(time.Second * 1)
}