So I'm not sure where to ask this, but I'd figure I'd give this a shot. So I'm trying to get the File Handle on a socket in windows.
Using like the following:
address, err := net.ResolveTCPAddr("tcp", "192.168.9.144:8080")
if err != nil {
fmt.Println(err)
}
conn, err := net.DialTCP("tcp", nil, address)
fmt.Println("Connected")
// conn, err := net.DialTCP("tcp", nil, address)
if err != nil {
fmt.Println("Can't conenct")
log.Fatal(err)
}
conn.File()
conn.Fd()
conn.File() Fails and states it's not supported by windows
Next I tried to use the windows api function _get_osfhandle
var (
procGetOSfHandle = msvcrt.NewProc("_get_osfhandle")
)
func GetOSfHandle(fd uintptr) (uintptr, error) {
value, _, err := procGetOSfHandle.Call(fd)
return value, err
}
hand, _ := GetOSfHandle(uintptr(unsafe.Pointer(conn)))
hand returns 4294967295 which turns out to be not real as in "xff\xff\xff\xff"
I then tried just to use the windows API for socket and ran into an issue with WSARecv. This is the following code used:
var d syscall.WSAData
syscall.WSAStartup(uint32(0x202), &d)
fd, _ := syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
addr := syscall.SockaddrInet4{Port: 8080, Addr: [4]byte{192, 168, 9, 144}}
var buf [4]byte
syscall.Connect(fd, &addr)
dataBuf := syscall.WSABuf{Len: uint32(4), Buf: &buf[0]}
o := syscall.Overlapped{}
flags := uint32(0)
qty := uint32(0)
syscall.WSARecv(fd, &dataBuf, 1, &qty, &flags, &o, nil)
fmt.Println(buf)
The code connects but I don't see and data received. It seems I don't seem to have the WSARecv options correct.
Any help would be greatly appreciated.
评论:
nyoungman:
scarboyontheinternet:So I'm not sure where to ask this
You might also try Stack Overflow and the mailing list.
joeshaw:It's gross, but look at sysfd() in this package. https://github.com/golang/net/blob/master/ipv4/helper_windows.go
Keep in mind that since it's accessing unexported fields using reflection this can change at any time in the future.
You might also want to try looking at golang.org/x/sys, which is where any new syscall development is occurring.
https://github.com/golang/sys/blob/master/windows/syscall_windows.go