如何在go编程语言中使用WSASocket函数创建套接字?
问题描述:
有没有人知道如何在编程语言中创建WSASocket()函数返回的SOCKET?如何在go编程语言中使用WSASocket函数创建套接字?
使用正常的syscall.Socket类型syscall.Bind结果为: WSAENOTSOCK - 错误10038 - 尝试对非socket的东西进行操作。指定的套接字参数是指文件,而不是套接字。
感谢
答
我们不使用这种低层次的API,我们使用net.Dial。恩。
func main() {
var (
host = "127.0.0.1"
port = "9998"
remote = host + ":" + port
msg string = "test"
)
con, error := net.Dial("tcp4", remote)
if error != nil {
fmt.Printf("Host not found: %s\n", error)
os.Exit(1)
} else {
defer con.Close()
}
in, error := con.Write([]byte(msg))
if error != nil {
fmt.Printf("Error sending data: %s, in: %d\n", error, in)
os.Exit(2)
}
fmt.Println("Connection OK")
}
或者,你可以跟踪代码$ GOROOT/src目录/包装/网/ dial.go