You want to use Recv() and Send() not Read() and Write() in order to get the address information.
Read() and Write() are for connected sockets and usually streaming. > On Feb 15, 2020, at 9:27 AM, Robert Engels <[email protected]> wrote: > > > Sorry. I missed the part about “domain” socket. > >>> On Feb 15, 2020, at 9:26 AM, Robert Engels <[email protected]> wrote: >>> >> >> See github.com/robaho/go-trader for an example of sending / receiving udp >> traffic. There’s no reason to use the low level Unix interfaces. >> >>>> On Feb 15, 2020, at 9:22 AM, Gert <[email protected]> wrote: >>>> >>> >>> Hi, leaving out a lot of details I basically do this in a C client >>> >>> fd = socket(PF_UNIX, SOCK_DGRAM, 0) >>> bind(fd, (struct sockaddr*) &client, sizeof(struct sockaddr_un) >>> connect(fd, (struct sockaddr*) &server, sizeof(struct sockaddr_un) >>> send(fd, buff, strlen(buff)+1, 0) >>> recv(fd, buff, 8192, 0) >>> >>> Which means I have a client socket where I receive datagrams and a server >>> socket where I sent datagrams to. >>> >>> It works in my C client above but having troubles understanding how to talk >>> to the same C server using a Go client instead. This is my attempt >>> >>> >>> addr, err := net.ResolveUnixAddr("unixgram", "client.sock") >>> if err != nil { >>> log.Fatal(err) >>> } >>> >>> client, err := net.ListenUnixgram("unixgram", addr) >>> if err != nil { >>> log.Fatal(err) >>> } >>> defer client.Close() >>> >>> server, err := net.Dial("unixgram", "server.sock") >>> if err != nil { >>> log.Fatal(err) >>> } >>> defer server.Close() >>> >>> _, err = server.Write([]byte("hi")) >>> if err != nil { >>> log.Fatal(err) >>> } >>> >>> buf := make([]byte, 1024) >>> n, err := client.Read(buf[:]) >>> if err != nil { >>> println(err) >>> } >>> println(string(buf[0:n])) >>> >>> if err := os.RemoveAll("client.sock"); err != nil { >>> log.Fatal(err) >>> } >>> >>> >>> What happens in the Go implementation is that it successfully send a >>> datagram, the server receives it but then the server can't figure out where >>> to send a answer back with a error from the server sendto: `No such file or >>> directory` >>> >>> This is basically what the the C server code is doing. >>> >>> recvfrom(fd, buff, 8192, 0, (struct sockaddr*) &client, &len) >>> sendto(fd, buff, strlen(buff)+1, 0, (struct sockaddr*) &client, len) >>> >>> My socket knowledge is limited and https://golang.org/pkg/net/ is >>> overwhelming, if I would make a guess is that de C client shares fd between >>> bind and connect and my Go implementation doesn't ? >>> >>> >>> -- >>> You received this message because you are subscribed to the Google Groups >>> "golang-nuts" group. >>> To unsubscribe from this group and stop receiving emails from it, send an >>> email to [email protected]. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/golang-nuts/644fa128-96c9-47f8-abc5-5fcc19d01e6e%40googlegroups.com. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/447FF9B8-9567-4472-84C7-B3B741CE1D0E%40ix.netcom.com.
