Hi everyone,
I am sorry for my newbie question.
I want to study law-level socket programming in go using system calls (such as to implement ARP).
For my first trial I have tested the code below named socket.go on Mac OS X 10.11.5 (darwin amd64),
but I got the following error:
# go run packet.go
./socket.go:17: cannot use sa (type unix.SockaddrInet4) as type unix.Sockaddr in argument to unix.Connect:
unix.SockaddrInet4 does not implement unix.Sockaddr (unix.sockaddr method has pointer receiver)
So I tested the same code on Linux VM (Arch Linux), but I got the same error.
My questions:
- Why my code causes the error in Linux?
- Is it impossible to use unix.SockaddrInet4 as type unix.Sockaddr in darwin?
Thank you.
----
packet.go
----
package main
import (
"fmt"
"os"
)
func main() {
fd, err := unix.Socket(unix.AF_INET, unix.SOCK_RAW, 0)
if err != nil {
fmt.Println("Can't open the socket:", err)
os.Exit(1)
}
sa := unix.SockaddrInet4{Port: 0, Addr: [4]byte{127, 0, 0, 1}}
err = unix.Connect(fd, sa)
if err != nil {
fmt.Println("Can't connect:", err)
os.Exit(1)
}
}