ListenAndServe返回net.DNSError错误:”未提供节点名或服务名”

huangapple go评论83阅读模式
英文:

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:
    - &quot;3306:3306&quot;
phpmyadmin:
  image: phpmyadmin/phpmyadmin:latest
  links:
  - &quot;mysql:db&quot;
  ports:
  - &quot;8080:80&quot;

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{&quot;*&quot;},
	AllowedMethods:   []string{&quot;GET&quot;, &quot;POST&quot;, &quot;PUT&quot;, &quot;DELETE&quot;},
	AllowedHeaders:   []string{&quot;*&quot;},
	AllowCredentials: true,
}))

external := &amp;http.Server{
	Addr:    &quot;10001&quot;,
	Handler: stack,
}

// Start the server, listening for incomming connections.
go func() {
	err = external.ListenAndServe()
	if err != nil &amp;&amp; err != http.ErrServerClosed {
		level.Error(logger).Log(&quot;msg&quot;, &quot;error while listening&quot;, &quot;err&quot;, 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 := &amp;Server{Addr: addr, Handler: handler}
    return server.ListenAndServe()
}

Can you try?

external := http.Server{
    Addr:    &quot;:10001&quot;,
    Handler: stack,
}

huangapple
  • 本文由 发表于 2017年6月19日 17:33:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/44626931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定