去监听已经使用的端口,不返回错误

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.

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.

both apps listening to 0.0.0.0:80

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

test.go

package main

import (
    "net/http"
    "log"
)

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

testing

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

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(":80", nil); err != nil {
    panic(err)
}