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

如何填写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)
}

图片转代码服务由CSDN问答提供 功能建议

使用Windows,我有一个syscall.handle的袜子,我想绑定到syscall中指定的地址

。如何填写syscall.Sockaddr结构,以便以后与syscall.Bind结合使用?

BTW,sock是一个原始套接字。

  var sa syscall.Sockaddr //如何用我的IP地址填写此结构?  ?
 
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.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题