dsm0688 2011-11-02 19:24
浏览 335
已采纳

如何填写syscall.Sockaddr结构,以便以后可以与syscall.Bind一起使用?

Using go on Windows I have a syscall.Handle called sock that I want to bind to an address specified in a syscall.Sockaddr structure called "sa".

How do I fill out the syscall.Sockaddr structure so that I can later use it with syscall.Bind?

BTW, sock is a raw socket.

var sa syscall.Sockaddr // how to fill out this structure with my ip address???

e := syscall.Bind(sock, sa)

if e != nil {
    fmt.Println("bind error", e)
}
  • 写回答

1条回答 默认 最新

  • doulue7522 2013-01-15 04:17
    关注

    A good rule of thumb when you're unsure of something in go is "check the source", especially where the syscall stuff is concerned. I suspect the lack of documentation is semi-intentional because as previously mentioned, it's not the recommended interface - better to use the net package for a portable solution :)

    Once you get to the source ($GOROOT/src/pkg/syscall/syscall_windows.go), you'll notice that Sockaddr isn't a struct at all:

    type Sockaddr interface {
            sockaddr() (ptr uintptr, len int32, err error) // lowercase; only we can define Sockaddrs
    }
    

    So lets look for structs that implement this interface:

    func (sa *SockaddrInet4) sockaddr() (uintptr, int32, error) {
    func (sa *SockaddrInet6) sockaddr() (uintptr, int32, error) {
    func (sa *SockaddrUnix) sockaddr() (uintptr, int32, error) {
    

    The Inet6 and Unix implementations are just stubs in the version I'm looking at (1.0.2), but SockaddrInet4 is ready to go:

    type SockaddrInet4 struct {
            Port int
            Addr [4]byte
            raw  RawSockaddrInet4
    }
    

    Just need to fill in Port and Addr.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料