Go服务器无法接收远程请求。

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

Go server not hearing remote requests

问题

我写了一个 Go 服务器,只要你从本地主机发送请求(并且发送到本地主机),它就能完美运行,但是当你尝试从浏览器(从另一台计算机)或者直接使用外部 IP 地址访问时,它就无法工作。我想要能够将其作为外部服务器访问,而不仅仅是本地访问。为什么它不能工作呢?

以下是(简化的)源代码:

package main

import (
	"fmt"
	"net"
	"os"
)

func main() {
	// 监听传入的连接。
	l, err := net.Listen("tcp", "localhost:2082")
	if err != nil {
		fmt.Println("Error listening:", err.Error())
		os.Exit(1)
	}
	// 应用程序关闭时关闭监听器。
	defer l.Close()

	for {
		// 监听传入的连接。
		_, err := l.Accept()
		if err != nil {
			fmt.Println("Error accepting: ", err.Error())
			os.Exit(1)
		}
		fmt.Println("Incoming connection")
	}
}

当你使用 curl localhost:2082 时,它会显示 "Incoming connection"。
当你使用 curl mydomain.com:2082 时,它没有任何反应。

端口已经转发了。我确定这一点,因为我从该端口运行了一个(node.js)Web 服务器,它可以正常工作。如果这相关的话,我在亚马逊 EC2 实例上运行的是 Ubuntu 12.04。

我会很感激任何帮助。谢谢!

英文:

I've written a Go server that works perfectly as long as you send it requests from localhost (and addressed to localhost), but it doesn't work when you try to access it from a browser (from a different computer) or even just directed at the external IP address. I want to be able to access it as an external server, not just locally. Why can't it?

The (pared down) source code:

package main

import (
	"fmt"
	"net"
	"os"
)

func main() {
	// Listen for incoming connections.
	l, err := net.Listen("tcp", "localhost:2082")
	if err != nil {
		fmt.Println("Error listening:", err.Error())
		os.Exit(1)
	}
	// Close the listener when the application closes.
	defer l.Close()

	for {
		// Listen for an incoming connection.
		_, err := l.Accept()
		if err != nil {
			fmt.Println("Error accepting: ", err.Error())
			os.Exit(1)
		}
		fmt.Println("Incoming connection")
	}
}

When you curl localhost:2082, it says "Incoming connection".
When you curl mydomain.com:2082, it does nothing.

The port is forwarded. I'm sure of this because I ran a (node.js) web server from that port, and it worked fine. If it's related, I'm running on Ubuntu 12.04 on an Amazon EC2 instance.

I'd appreciate any help. Thanks!

答案1

得分: 8

一种监听任何传入的 IP(不仅限于默认映射到 127.0.0.1 的 localhost)的方法是:

net.Listen("tcp", ":2082")

你还可以使用 net/http/#ListenAndServe 函数,如果需要的话,可以触发监听多个特定的 IP。

go http.ListenAndServe("10.0.0.1:80", nil)
http.ListenAndServe("10.0.0.2:80", nil)

在 "A Recap of Request Handling in Go" 中可以找到一个很好的示例。

英文:

One way to listen to any incoming IP (not just localhost, mapped by default to 127.0.0.1) would be:

net.Listen("tcp", ":2082")

You also have the function net/http/#ListenAndServe, which allows you to trigger listen on multiple specific ip if you want.

go http.ListenAndServe("10.0.0.1:80", nil)
http.ListenAndServe("10.0.0.2:80", nil) 

A good example can be seen in "A Recap of Request Handling in Go".

huangapple
  • 本文由 发表于 2014年7月20日 03:09:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/24843995.html
匿名

发表评论

匿名网友

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

确定