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

在 PowerShell 中输入:netstat -ano,查看所有的端口占用情况,看到程序中的端口(4000)已经在另一个程序中处于活动状态(正在使用中)
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
在 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
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
在 PowerShell 中输入:netstat -ano,查看所有的端口占用情况,看到程序中的端口(4000)已经在另一个程序中处于活动状态(正在使用中)
Figure 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. 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
端口 4000 被进程号为 313996 的进程占用,打开 任务管理器 ,找到 Pid 为 313996 的进程,选择 - 结束任务 - 结束进程
Figure 3
5. Run again in powershell: go run web-servers.go, no error is reported


PS E:\wwwroot\go\methods> go run web-servers.go



 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.