C - 为什么我无法从外部本地主机访问我的服务器?
我最近一直在从事套接字编程;我目前正在制作一台服务器,该服务器侦听端口上的传入连接,然后一旦获得连接,就读取字符串并将其放入文件中。
当我从本地主机远程登录到正在运行的服务器时,一切正常,但是当我尝试从其他任何地方访问它时,它的行为就好像它不存在一样。 Nmap 不显示来自本地主机或远程主机的特定开放端口。它仅通过本地主机上的 telnet 工作。
该函数的代码位于此处。 (Pastebin) 我知道这很混乱,我对网络编程还很不熟悉。确实没有任何好的深入教程可以向您展示您需要了解的所有内容。看来我得买本书了...
I've been working with socket programming lately; and I'm currently making a server that listens on a port for incoming connections, then once it gets one, reads in a string and places it in a file.
It all works fine when I'm telneting to the running server from localhost, but when I attempt to access it from anywhere else, it acts as though it doesn't exist. Nmap doesn't show that particular open port -- from either the localhost or the remote host. It only works through telnet on localhost.
The function's code is here. (Pastebin)
I know it's a mess, I'm still pretty unfamiliar with network programming. There really aren't any good in-depth tutorials that show you everything you need to know. I guess I'll have to buy a book...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码正在调用
b
,其中b
基于调用getaddrinfo
的结果。这可能不完全包含您想要的内容。看起来cliaddr
已经包含要传递给bind()
的正确数据,因此请使用它:我不确定
getaddrinfo()
是什么> 可能会为您返回,但从您的描述来看,它听起来可能提供地址 127.0.0.1 (这是本地主机)。如果您仅bind()
到本地主机接口,那么这是唯一响应连接请求的地址。如果您绑定到INADDR_ANY
,那么所有接口都会响应连接请求。对于简单的套接字侦听程序,您可能根本不需要调用
getaddrinfo()
。Your code is calling
where
b
is based on the result of callinggetaddrinfo
. This probably doesn't contain exactly what you want. It looks likecliaddr
already contains the correct data to pass tobind()
, so use that instead:I'm not sure what
getaddrinfo()
might return for you, but it sounds from your description like it might be providing the address 127.0.0.1 (which is localhost). If youbind()
only to the localhost interface, then that's the only address that will respond to request to connect. If you bind toINADDR_ANY
, then all interfaces will respond to requests for connection.For a simple socket listening program, you probably don't need the call to
getaddrinfo()
at all.我的猜测是您的端口被防火墙阻止了。尝试通过 22580 端口 telnet 到服务器并查看是否有响应。如果是这样,则服务器和您的代码之间存在问题。我的猜测是它会超时并且不应答,因此问题出在您的服务器和网络之间。
大多数网络会阻止大多数端口,只开放 80、443、25、8080 和其他一些端口。
telnet server.ip.address 22580
并查看是否连接。
My guess is that your port is blocked by a firewall. Try telnet to the server via the 22580 port and see if it answers. If it does, then there's a problem between the server and your code. My guess is that it will timeout and not answer, so the problem is between your server and the network.
Most networks will block most ports, only opening port 80, 443, 25, 8080 and a few others.
telnet server.ip.address 22580
and see if it connects.
可能需要更多信息,因为导致此失败的可能原因有很多。如果从互联网访问(和映射),您可能需要通过防火墙配置“打孔”以允许访问。否则,侦听器主机上的“netstat”和“tcpdump”等工具可能会提供更有用的信息。
More information may be needed since there are many possible causes for this to fail. If accessing (and nmapping) from the internet, you may need to "punch a hole" through your firewall configuration to allow access. Otherwise tools like "netstat" and "tcpdump" on the listener host may provide more useful information.