监听TCP4而不是TCP6 [英] Listen on TCP4 not TCP6

查看:377
本文介绍了监听TCP4而不是TCP6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 https://github.com/gin-gonic/gin 编写一个http服务但是当我部署它时,它会继续部署在tcp6上(根据netstat)

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")

推荐答案

文档说明

Run将路由器附加到http.Server,并开始侦听和处理HTTP请求.这是http.ListenAndServe(addr,router)

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

您可以使用 http.Server 直接启动服务器,就像 http 包在 ListenAndServe

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)
// ...

这篇关于监听TCP4而不是TCP6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆