There is no problem not worth solving, and no technology not worth learning!
In the Go Guide, Web server, error report: Listen TCP 127.0.0.1:4000: BIND: only one usage of each socket address (Protocol/Network Address/Port) Is Normally Permitted Analysis Solution
1. Refer to the Go guide, web server, write file: web-servers.go
package main
import (
"fmt"
"log"
"net/http"
)
type Hello struct{}
func (h Hello) ServeHTTP(
w http.ResponseWriter,
r *http.Request) {
fmt.Fprint(w, "Hello!")
}
func main() {
var h Hello
err := http.ListenAndServe("localhost:4000", h)
if err != nil {
log.Fatal(err)
}
}
2. Run in PowerShell: go run web-servers.go, report error: listen tcp 127.0.0.1:4000: bind: only one usage of each Socket address (protocol/network address/port) is normally permitted, as shown in Figure 1
Figure 1
PS E:\wwwroot\go\methods> go run web-servers.go
2020/01/09 16:13:46 listen tcp 127.0.0.1:4000: bind: Only one usage of each socket address (protocol/network address/por
t) is normally permitted.
exit status 1
3. Enter: netstat -ano in PowerShell, check all the port occupancy, and see that the port (4000) in the program is already active (in use) in another program. as shown in Figure 2
Figure 2
4. Port 4000 is occupied by the process number 313996, open the task manager, find the process with pid 313996, select – end task – end the process, as shown in Figure 3
Figure 3
5. Run again in powershell: go run web-servers.go, no error is reported
Leave a Reply