英文:
ListenAndServe returns net.DNSError "nodename nor servname provided"
问题
我正在尝试编写一个Go的HTTP服务器,但是当我尝试监听传入连接时遇到了问题。
硬件设置:MacOS Sierra 10.12.4,docker 17.03
我有一个yml文件,将打开一个.phpmyadmin和sql:
mysql:
extends:
service: mysql-5.7
ports:
- "3306:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
links:
- "mysql:db"
ports:
- "8080:80"
这似乎工作正常。
之后,我编写了一个Go文件来连接并监听连接:
stack := negroni.New()
stack.Use(cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
AllowedHeaders: []string{"*"},
AllowCredentials: true,
}))
external := &http.Server{
Addr: "10001",
Handler: stack,
}
// 启动服务器,监听传入连接。
go func() {
err = external.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
level.Error(logger).Log("msg", "error while listening", "err", err)
os.Exit(1)
}
}()
这将生成以下错误:
level=error time=2017-06-19T09:28:14.726843314Z app="Falbala" env=local pid=1084 host=P5RO-PORT367.local msg="error while listening" err="listen tcp: lookup tcp/10001": nodename nor servname provided, or not known
有什么想法吗?谢谢。
英文:
<br>
I am trying to write a go http server but I have an issue when I try to listen to incoming connections. <br>
HW setup: MacOS Sierra 10.12.4, docker 17.03
<br>I have a yml file that will open a . phpmyadmin and sql:
mysql:
extends:
service: mysql-5.7
ports:
- "3306:3306"
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
links:
- "mysql:db"
ports:
- "8080:80"
This seems to work fine.
After this I wrote a go file to connect and listen to connections:
stack := negroni.New()
stack.Use(cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"},
AllowedHeaders: []string{"*"},
AllowCredentials: true,
}))
external := &http.Server{
Addr: "10001",
Handler: stack,
}
// Start the server, listening for incomming connections.
go func() {
err = external.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
level.Error(logger).Log("msg", "error while listening", "err", err)
os.Exit(1)
}
}()
This will generate the error:
> level=error time=2017-06-19T09:28:14.726843314Z app=“Falbala” env=local pid=1084 host=P5RO-PORT367.local msg="error while listening" err="listen tcp: lookup tcp/10001”: nodename nor servname provided, or not known"
Any ideas?<br>
Thank you
答案1
得分: 2
根据文档,地址值应该是一个字符串。
// ListenAndServe 总是返回非nil的错误。
func ListenAndServe(addr string, handler Handler) error {
server := &Server{Addr: addr, Handler: handler}
return server.ListenAndServe()
}
你可以试试看吗?
external := http.Server{
Addr: ":10001",
Handler: stack,
}
英文:
From the documentation, address value should be a string.
// ListenAndServe always returns a non-nil error.
func ListenAndServe(addr string, handler Handler) error {
server := &Server{Addr: addr, Handler: handler}
return server.ListenAndServe()
}
Can you try?
external := http.Server{
Addr: ":10001",
Handler: stack,
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论