gpt4 book ai didi

windows - 如何在Windows上使用Go从套接字fd读取/写入套接字fd?

转载 作者:行者123 更新时间:2023-12-01 22:35:48 25 4
gpt4 key购买 nike

我一直在尝试syscall.Socket创建fd,调用syscall.Connectsyscall.Write()syscall.Write()返回错误The parameter is incorrect.

最佳答案

package main

import (
"errors"
"fmt"
"os"
"syscall"
)

func main() {
fd, _ := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_TCP)

addr := syscall.SockaddrInet4{
Port: 30001,
Addr: [4]byte{172, 16, 1, 222},
}

connectErr := syscall.Connect(fd, &addr)

defer syscall.Close(fd)

if connectErr != nil {
fmt.Println(connectErr)
return
}

writeLen, err := syscall.Write(fd, []byte("test"))

/* file, err := MakeTunFile(uintptr(fd))
if err != nil {
fmt.Println(err)
return
}

writeLen, err := file.WriteString("test") */

fmt.Println(writeLen) // 0
fmt.Println(err) // The parameter is incorrect.
}

// MakeTunFile returns an os.File object from a TUN file descriptor `fd`.
func MakeTunFile(fd uintptr) (*os.File, error) {
if fd < 0 {
return nil, errors.New("TUN file descriptor error")
}
file := os.NewFile(fd, "")
if file == nil {
return nil, errors.New("open TUN file descriptor error")
}
return file, nil
}

关于windows - 如何在Windows上使用Go从套接字fd读取/写入套接字fd?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63450202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com