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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测