当前位置:网站首页>When you run the demo using the gin framework, there is an error "listen TCP: 8080: bind: an attempt was made to access a socket in a way forbidden"

When you run the demo using the gin framework, there is an error "listen TCP: 8080: bind: an attempt was made to access a socket in a way forbidden"

2022-06-26 01:09:00 vivisol

Problem description

Trying to run Gin The framework is officially provided demo when , Report errors : listen tcp :8080: bind: An attempt was made to access a socket in a way forbidden by its access permissions.

[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /ping                     --> main.main.func1 (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080
[GIN-debug] [ERROR] listen tcp :8080: bind: An attempt was made to access a socket in a way forbidden by its access permissions.

reason

Official DEMO as follows :

package main

import "github.com/gin-gonic/gin"

func main() {
    
	r := gin.Default()
	r.GET("/ping", func(c *gin.Context) {
    
		c.JSON(200, gin.H{
    
			"message": "pong",
		})
	})
	r.Run() //  Monitor and  0.0.0.0:8080  Upstart service 
}

Because the official DEMO The default is 8080 port , The local 8080 The port is occupied by other programs, resulting in failure

terms of settlement

Replace DEMO Port in , For example, change to 8080:

package main

import "github.com/gin-gonic/gin"

func main() {
    
	r := gin.Default()
	r.GET("/ping", func(c *gin.Context) {
    
		c.JSON(200, gin.H{
    
			"message": "pong",
		})
	})
	r.Run(":8000") //  Monitor and  0.0.0.0:8080  Upstart service 
}

Running effect

 Insert picture description here

原网站

版权声明
本文为[vivisol]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206252313486691.html