go也可以像普通的socket编程那样: 创建套接字 -> 绑定 -> 监听 …

当然,go中有net包可以使用,但是如果要实现一些比较底层的操作,例如自己构造数据包,就可以通过这种比较原始的方式来进行socket编程。


代码如下:

package main

import (
    . "fmt"
    "strconv"
    "strings"
    "syscall"
)

func MAKEWORD(low, high uint8) uint32 {
    var ret uint16 = uint16(high)<<8 + uint16(low)
    return uint32(ret)
}

func inet_addr(ipaddr string) [4]byte {
    var (
        ips = strings.Split(ipaddr, ".")
        ip  [4]uint64
        ret [4]byte
    )
    for i := 0; i < 4; i++ {
        ip[i], _ = strconv.ParseUint(ips[i], 10, 8)
    }
    for i := 0; i < 4; i++ {
        ret[i] = byte(ip[i])
    }
    return ret
}

func main() {
    var (
        sock    syscall.Handle
        addr    syscall.SockaddrInet4
        wsadata syscall.WSAData
        err     error
    )
    if err = syscall.WSAStartup(MAKEWORD(2, 2), &wsadata); err != nil {
        Println("Startup error")
        return
    }
    defer syscall.WSACleanup()

    if sock, err = syscall.Socket(syscall.AF_INET, syscall.SOCK_STREAM, syscall.IPPROTO_IP); err != nil {
        Println("Socket create error")
        return
    }
    defer syscall.Closesocket(sock)

    addr.Addr = inet_addr("61.135.169.105")
    addr.Port = 80
    if err = syscall.Connect(sock, &addr); err != nil {
        Println("Connect error")
        return
    }

    var (
        data       syscall.WSABuf
        sendstr    string = "hello"
        SendButes  uint32
        overlapped syscall.Overlapped
    )
    data.Len = uint32(len(sendstr))
    data.Buf = syscall.StringBytePtr(sendstr)
    //如果使用syscall.Sendto或syscall.Write会发送失败,原因未知 
    err = syscall.WSASend(sock, &data, 1, &SendButes, 0, &overlapped, nil)
    if err != nil {
        Println("Send error")
    } else {
        Println("Send success")
    }
}


注意:syscall.Sendto和syscall.Write两个函数试过了会发送失败,而且我不知道什么原因,在windows环境下看来只能使用WSA****系列的函数进行发送数据包了,不过证明了Go也可以进行底层的网络编程而不借助Cgo