Windows syscall

polaris · · 3831 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>So I&#39;m not sure where to ask this, but I&#39;d figure I&#39;d give this a shot. So I&#39;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(&#34;tcp&#34;, &#34;192.168.9.144:8080&#34;) if err != nil { fmt.Println(err) } conn, err := net.DialTCP(&#34;tcp&#34;, nil, address) fmt.Println(&#34;Connected&#34;) // conn, err := net.DialTCP(&#34;tcp&#34;, nil, address) if err != nil { fmt.Println(&#34;Can&#39;t conenct&#34;) log.Fatal(err) } conn.File() conn.Fd() </code></pre> <p>conn.File() Fails and states it&#39;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(&#34;_get_osfhandle&#34;) ) 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 &#34;xff\xff\xff\xff&#34;</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), &amp;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, &amp;addr) dataBuf := syscall.WSABuf{Len: uint32(4), Buf: &amp;buf[0]} o := syscall.Overlapped{} flags := uint32(0) qty := uint32(0) syscall.WSARecv(fd, &amp;dataBuf, 1, &amp;qty, &amp;flags, &amp;o, nil) fmt.Println(buf) </code></pre> <p>The code connects but I don&#39;t see and data received. It seems I don&#39;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&#39;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&#39;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&#39;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

3831 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传