Windows syscall

polaris · 2015-04-08 18:20:38 · 3884 次点击    
这是一个分享于 2015-04-08 18:20:38 的资源,其中的信息可能已经有所发展或是发生改变。

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:

So I'm not sure where to ask this

You might also try Stack Overflow and the mailing list.

http://stackoverflow.com/questions/tagged/go

https://groups.google.com/forum/#!forum/golang-nuts

scarboyontheinternet:

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.

joeshaw:

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


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

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