监听已被占用的端口时不返回错误

huangapple go评论76阅读模式
英文:

Go listen on port which is already in use not returning error

问题

我已经有一个在端口80上监听的Web服务器(使用Node.js编写)。当我运行另一个也在端口80上监听的Web服务器(使用Go编写)时,Go Web服务器不会引发错误。

这是如何发生的?

我的操作系统是Windows,Go版本是go1.2.2。

英文:

I already have a webserver which is listening on port 80 (written in node.js). When I run another webserver that also listens on port 80 (written in Go), the Go webserver doesn't raise an error.

How can this occur.

My OS is windows and go version go1.2.2.

答案1

得分: 8

我在工作中遇到过这个问题。Golang服务器将监听IPv6端口80,而其他应用程序只监听IPv4。

对我来说,Golang应用程序首先运行。当其他应用程序关闭时,它停止监听v4,然后恢复监听。

[稍后编辑]
为了演示这一点,我只需运行在此MSDN页面上找到的WinSock绑定/监听C++代码,并将端口更改为80,然后使用以下Go代码:

package main

import (
    "log"
    "net/http"
)

func main() {
    http.Handle("/", http.FileServer(http.Dir("c:\\temp")))
    log.Fatal(http.ListenAndServe(":80", nil))
}

这个设置可以工作,因为C++正在监听127.0.0.1,而Go在0.0.0.0上监听。将Go代码更改为log.Fatal(http.ListenAndServe("127.0.0.1:80", nil))会导致nemo建议的错误消息。

然后,我启动了我的主要生产代码,其中有一个Mongoose HTTP实例,它正在监听0.0.0.0:80,然后运行上述的Go代码(删除127.0.0.1),两者都在监听0.0.0.0:80,这可以通过进程资源管理器看到。

监听已被占用的端口时不返回错误

英文:

I had this happen at work. The golang server will be listening to the IPv6 port 80 while the other application is only listening to IPv4.

For me the golang app was running first. And it stop listening to v4 and then resumed once the other app was closed.

[edit later]
To demostrate this, I just ran the WinSock bind/listen C++ code found on this MSDN page with the port changed to 80, then I used this Go code:

package main

import (
    "log"
    "net/http"
)

func main() {
    http.Handle("/", http.FileServer(http.Dir("c:\\temp")))
    log.Fatal(http.ListenAndServe(":80", nil))
}

This setup worked because the C++ was listening to 127.0.0.1 and the Go on 0.0.0.0 Changing the go code to log.Fatal(http.ListenAndServe("127.0.0.1:80", nil)) caused the error message nemo suggested.

I then started my main production code, which has a Mongoose HTTP instance, and it's listening on 0.0.0.0:80, and then ran the above Go code (removing 127.0.0.1) and both are listening to 0.0.0.0:80, this can be seen via Process Explorer.

监听已被占用的端口时不返回错误

答案2

得分: 0

Go很好地报告错误。你可能忘记检查返回的错误值。

test.go

package main

import (
    "net/http"
    "log"
)

func main() {
    log.Fatal(http.ListenAndServe(":8080", nil))
}

测试

<!-- language: none -->

$ nc -l 8080
$ go run test.go
2014/06/30 08:40:49 listen tcp :8080: bind: address already in use
exit status 1
英文:

Go reports errors just fine. You're probably missing to check the returned error value.

test.go

package main

import (
	&quot;net/http&quot;
	&quot;log&quot;
)

func main() {
	log.Fatal(http.ListenAndServe(&quot;:8080&quot;, nil))
}

testing

<!-- language: none -->

$ nc -l 8080
$ go run test.go
2014/06/30 08:40:49 listen tcp :8080: bind: address already in use
exit status 1

答案3

得分: -2

你不能在同一个端口上运行两个应用程序,同时在调用函数时需要检查返回的错误:

如果 err := http.ListenAndServe(":80", nil); err != nil {
panic(err)
}

英文:

You can't run two applications on the same port, also you need to check for errors returned when calling functions :

if err := http.ListenAndServe(&quot;:80&quot;, nil); err != nil {
	panic(err)
}

huangapple
  • 本文由 发表于 2014年6月30日 14:18:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/24483955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定