Listen on TCP4 not TCP6

瘦欲@ 提交于 2019-12-06 05:22:28

问题


I am using https://github.com/gin-gonic/gin to write an http service But when I deploy it, it keeps deploying on tcp6(according to netstat)

r := gin.Default()
//none of these are working , It keeps being listed on tcp6
r.Run(":8080")
r.Run("*:8080")
r.Run("0.0.0.0:8080")

回答1:


The documentation states

Run attaches the router to a http.Server and starts listening and serving HTTP requests. It is a shortcut for http.ListenAndServe(addr, router)

You can start the server directly using an http.Server the same way the http package does in ListenAndServe

server := &http.Server{Handler: r}
l, err := net.Listen("tcp4", addr)
if err != nil {
    log.Fatal(err)
}
err = server.Serve(l)
// ...


来源:https://stackoverflow.com/questions/38592064/listen-on-tcp4-not-tcp6

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!