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.

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

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面