Go 指南中,Web 服务器,报错:listen tcp 127.0.0.1:4000: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted 的分析解决
1、参考 Go 指南中,Web 服务器,编写文件: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、在 PowerShell 中运行:go run web-servers.go,报错:listen tcp 127.0.0.1:4000: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted,如图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、在 PowerShell 中输入:netstat -ano,查看所有的端口占用情况,看到程序中的端口(4000)已经在另一个程序中处于活动状态(正在使用中)。如图2
PS E:\wwwroot\go\methods> netstat -ano 活动连接 协议 本地地址 外部地址 状态 PID TCP 0.0.0.0:80 0.0.0.0:0 LISTENING 313324 TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 1104 TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4 TCP 0.0.0.0:3306 0.0.0.0:0 LISTENING 5520 TCP 0.0.0.0:4301 0.0.0.0:0 LISTENING 297232 TCP 0.0.0.0:5040 0.0.0.0:0 LISTENING 8872 TCP 0.0.0.0:5826 0.0.0.0:0 LISTENING 213692 TCP 0.0.0.0:6379 0.0.0.0:0 LISTENING 6436 TCP 0.0.0.0:7680 0.0.0.0:0 LISTENING 964 TCP 0.0.0.0:10001 0.0.0.0:0 LISTENING 212304 TCP 0.0.0.0:19531 0.0.0.0:0 LISTENING 5444 TCP 0.0.0.0:20531 0.0.0.0:0 LISTENING 7760 TCP 0.0.0.0:49664 0.0.0.0:0 LISTENING 888 TCP 0.0.0.0:49665 0.0.0.0:0 LISTENING 804 TCP 0.0.0.0:49666 0.0.0.0:0 LISTENING 1784 TCP 0.0.0.0:49667 0.0.0.0:0 LISTENING 2364 TCP 0.0.0.0:49668 0.0.0.0:0 LISTENING 4184 TCP 0.0.0.0:49671 0.0.0.0:0 LISTENING 880 TCP 127.0.0.1:4000 0.0.0.0:0 LISTENING 313996 TCP 127.0.0.1:4000 127.0.0.1:54674 CLOSE_WAIT 313996 TCP 127.0.0.1:4000 127.0.0.1:55244 CLOSE_WAIT 313996 TCP 127.0.0.1:4000 127.0.0.1:55279 CLOSE_WAIT 313996 TCP 127.0.0.1:4000 127.0.0.1:55291 CLOSE_WAIT 313996 TCP 127.0.0.1:4000 127.0.0.1:55298 CLOSE_WAIT 313996 TCP 127.0.0.1:4000 127.0.0.1:55306 CLOSE_WAIT 313996 TCP 127.0.0.1:4000 127.0.0.1:55442 ESTABLISHED 313996 TCP 127.0.0.1:4012 0.0.0.0:0 LISTENING 7804 TCP 127.0.0.1:4013 0.0.0.0:0 LISTENING 7804
4、端口 4000 被进程号为 313996 的进程占用,打开 任务管理器 ,找到 Pid 为 313996 的进程,选择 – 结束任务 – 结束进程,如图3
5、在 PowerShell 中再次运行:go run web-servers.go,未报错
PS E:\wwwroot\go\methods> go run web-servers.go
近期评论