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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏