dqgg25493 2017-06-27 21:47
浏览 130
已采纳

“ golang.org/x/net/ipv4”在Mac上有效,但在Linux上无效

I'm using "golang.org/x/net/ipv4" in order to use its SetTTL function. Unfortunately it does not seem to work on Linux, only on Mac, even though the documentation indicates Linux supports all the functions.

Here's a minimal example of the problem, with a Dockerfile:

main.go:

package main

import (
  "fmt"
  "net"
  "bufio"
  xnet "golang.org/x/net/ipv4"
)

const Host = "google.com"

func main() {
  var err error
  conn, err := net.Dial("tcp4", Host + ":80")
  if err != nil {
    panic(err)
  }
  defer conn.Close()
  xconn := xnet.NewConn(conn)
  err = xconn.SetTTL(5)
  if err != nil {
    panic(err)
  }
  defer xconn.Close()
  fmt.Fprint(conn, "GET / HTTP/1.1
HOST: google.com

")
  firstLine, err := bufio.NewReader(xconn).ReadString('
')
  if err != nil {
    panic(err)
  }
  fmt.Println(firstLine)
}

Dockerfile:

FROM golang:1.8.1-alpine

RUN apk --no-cache add git
RUN go get golang.org/x/net/ipv4
COPY . /go/src/me.com/me/xnetproblem
RUN go install me.com/me/xnetproblem
CMD ["/go/bin/xnetproblem"]

I run this command:

docker build -t xnet .

I get this output:

john xnetproblem > docker build -t xnet .
Sending build context to Docker daemon 90.62 kB
Step 1/6 : FROM golang:1.8.1-alpine
[snip]
Step 5/6 : RUN go install me.com/me/xnetproblem
 ---> Running in c3802fe61d63
# me.com/me/xnetproblem
src/me.com/me/xnetproblem/main.go:25: xconn.Close undefined (type *ipv4.Conn has no field or method Close)
src/me.com/me/xnetproblem/main.go:28: cannot use xconn (type *ipv4.Conn) as type io.Reader in argument to bufio.NewReader:
    *ipv4.Conn does not implement io.Reader (missing Read method)
The command '/bin/sh -c go install me.com/me/xnetproblem' returned a non-zero code: 2

Using go install natively, instead of Docker, the program works on Mac but not on Linux.

  • 写回答

1条回答 默认 最新

  • dongzhui9936 2017-06-27 22:28
    关注

    Thanks to @JimB's comment I realized my Mac had an old version of the ipv4 package installed. After updating I was able to fix the code.

    Here's a complete working version:

    package main
    
    import (
      "fmt"
      "net"
      "bufio"
      "golang.org/x/net/ipv4"
    )
    
    const Host = "google.com"
    
    func main() {
      var err error
      conn, err := net.Dial("tcp4", Host + ":80")
      if err != nil {
        panic(err)
      }
      defer conn.Close()
    
      if err = ipv4.NewConn(conn).SetTTL(5); err != nil {
          panic(err)
      }
    
      fmt.Fprint(conn, fmt.Sprintf("GET / HTTP/1.1
    Host: %v
    
    ", Host))
      firstLine, err := bufio.NewReader(conn).ReadString('
    ')
      if err != nil {
        panic(err)
      }
      fmt.Println(firstLine)
    }
    

    Here is the output:

    HTTP/1.1 301 Moved Permanently
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多台相同IP产品如何组网
  • ¥30 关于php页面调取、更新数据提交刷新等问题
  • ¥50 使用 java+selenium 接管手动打开的IE浏览器
  • ¥15 ios 新安装app收不到fcm推送
  • ¥15 有没有实力的写手?有过成品的优先
  • ¥15 图像信息库的建立与识别
  • ¥15 韩国网站购物,KG支付的支付回调如何解决
  • ¥15 workstation导入ovf文件,报错,怎么解决呢?
  • ¥15 关于#c语言#的问题:构成555单稳态触发器,采用LED指示灯延时时间,对延时时间进行测量并显示(如楼道声控延时灯)需要Proteus仿真图和C语言代码
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,怎样解决呢?