duanjiaopi8218 2016-10-15 19:07
浏览 395

http.ListenAndServe仅适用于本地主机吗?

I've been successfully making use of

http.ListenAndServe(":80", mux)

to host my web service in Go. It only appears to work with localhost however.

http.ListenAndServe("192.168.1.83:80", mux)

This works for specific connections on this address but is there a way to make it work for any ip address on the server?

Edit:

I've checked it with a different port (8080 in this case) and then using ":8080" works as documented. There appears to be something special about port 80 even when testing on the same machine that means it only actually listens on localhost.

For the avoidance of doubt I'm using Windows and all testing is done on the same physical machine. I've also checked running with admin privileges and it makes no difference.

  • 写回答

2条回答 默认 最新

  • dragonsun00000 2016-10-15 19:20
    关注

    http.ListenAndServe(":80", mux) is the correct address. net/http uses the net package. Quoting from net.Listen():

    If host is omitted, as in ":8080", Listen listens on all available interfaces instead of just the interface with the given host address. See Dial for more details about address syntax.

    Know that port 80 is restricted or might be blocked by firewalls on many systems. On unix systems ports under 1024 usually require special permissions. Test the 8080 port for example, because that is not special in this way.

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集