<p>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.</p>
<p>Using like the following: </p>
<pre><code> 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()
</code></pre>
<p>conn.File() Fails and states it's not supported by windows</p>
<p>Next I tried to use the windows api function _get_osfhandle</p>
<pre><code>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)))
</code></pre>
<p>hand returns 4294967295 which turns out to be not real as in "xff\xff\xff\xff"</p>
<p>I then tried just to use the windows API for socket and ran into an issue with WSARecv. This is the following code used:</p>
<pre><code> 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)
</code></pre>
<p>The code connects but I don't see and data received. It seems I don't seem to have the WSARecv options correct.</p>
<p>Any help would be greatly appreciated. </p>
<hr/>**评论:**<br/><br/>nyoungman: <pre><blockquote>
<p>So I'm not sure where to ask this </p>
</blockquote>
<p>You might also try Stack Overflow and the mailing list. </p>
<p><a href="http://stackoverflow.com/questions/tagged/go" rel="nofollow">http://stackoverflow.com/questions/tagged/go</a></p>
<p><a href="https://groups.google.com/forum/#!forum/golang-nuts" rel="nofollow">https://groups.google.com/forum/#!forum/golang-nuts</a></p></pre>scarboyontheinternet: <pre><p>It's gross, but look at sysfd() in this package. <a href="https://github.com/golang/net/blob/master/ipv4/helper_windows.go" rel="nofollow">https://github.com/golang/net/blob/master/ipv4/helper_windows.go</a></p>
<p>Keep in mind that since it's accessing unexported fields using reflection this can change at any time in the future.</p></pre>joeshaw: <pre><p>You might also want to try looking at golang.org/x/sys, which is where any new syscall development is occurring.</p>
<p><a href="https://github.com/golang/sys/blob/master/windows/syscall_windows.go" rel="nofollow">https://github.com/golang/sys/blob/master/windows/syscall_windows.go</a></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传