dqyknf4423 2018-03-02 10:22
浏览 707
已采纳

收听“ 0.0.0.0:80”和“:80”有什么区别?

When we use http.ListenAndServe, what is the difference between:

http.ListenAndServe("0.0.0.0:80", nil)

and

http.ListenAndServe(":80", nil)

? Wouldn't both versions listen on all interfaces on port 80?

  • 写回答

1条回答 默认 最新

  • douqian1835 2018-03-02 12:44
    关注

    The http.ListenAndServe() function eventually calls net.Listen(). The documentation for net.Listen states that it will bind to the network provided to it:

    For TCP networks, if the host in the address parameter is empty or a literal unspecified IP address, Listen listens on all available unicast and anycast IP addresses of the local system. To only use IPv4, use network "tcp4".

    However, looking at the source for http.ListenAndServe() we can see that it specifies "tcp" as the network and not "tcp4". Therefore the call in your example code should result in identical behavior, i.e. both calls should bind to all available interfaces. However, digging further down into the golang source we end up in internetAddrList() and we can see that it differentiates between an empty host value and one that has a ipv4 address specified. So golang does infact treat the ipv4 address specified as an indication to only bind on that interface.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作