C - 为什么我无法从外部本地主机访问我的服务器?

发布于 2024-12-10 23:04:30 字数 359 浏览 0 评论 0原文

我最近一直在从事套接字编程;我目前正在制作一台服务器,该服务器侦听端口上的传入连接,然后一旦获得连接,就读取字符串并将其放入文件中。

当我从本地主机远程登录到正在运行的服务器时,一切正常,但是当我尝试从其他任何地方访问它时,它的行为就好像它不存在一样。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沙沙粒小 2024-12-17 23:04:30

您的代码正在调用

    if(bind(*sock, b->ai_addr, b->ai_addrlen) == -1) {

b ,其中 b 基于调用 getaddrinfo 的结果。这可能不完全包含您想要的内容。看起来 cliaddr 已经包含要传递给 bind() 的正确数据,因此请使用它:

    if (bind(*sock, (struct sockaddr *)&cliaddr, sizeof(cliaddr)) {

我不确定 getaddrinfo() 是什么> 可能会为您返回,但从您的描述来看,它听起来可能提供地址 127.0.0.1 (这是本地主机)。如果您仅 bind() 到本地主机接口,那么这是唯一响应连接请求的地址。如果您绑定到INADDR_ANY,那么所有接口都会响应连接请求。

对于简单的套接字侦听程序,您可能根本不需要调用getaddrinfo()

Your code is calling

    if(bind(*sock, b->ai_addr, b->ai_addrlen) == -1) {

where b is based on the result of calling getaddrinfo. This probably doesn't contain exactly what you want. It looks like cliaddr already contains the correct data to pass to bind(), so use that instead:

    if (bind(*sock, (struct sockaddr *)&cliaddr, sizeof(cliaddr)) {

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 you bind() only to the localhost interface, then that's the only address that will respond to request to connect. If you bind to INADDR_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.

早乙女 2024-12-17 23:04:30

我的猜测是您的端口被防火墙阻止了。尝试通过 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.

只是在用心讲痛 2024-12-17 23:04:30

可能需要更多信息,因为导致此失败的可能原因有很多。如果从互联网访问(和映射),您可能需要通过防火墙配置“打孔”以允许访问。否则,侦听器主机上的“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文