douwen8424 2015-04-08 21:06
浏览 108

Windows系统调用

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了