如何使用getaddrinfo使用外部IP连接到服务器?

Bak*_*riu 7 c sockets getaddrinfo

我正在编写一个小型的C客户端/服务器应用程序,但在使用外部IP地址时无法使连接正常工作.客户端和服务器的代码都来自这里,特别是客户端:

    char *default_server_name = "localhost";
    char *server_name = NULL;
    int nport = DEFAULT_DAMA_PORT;
    char port[15];

    // Parse the command line options
    if (parse_options(argc, argv, &server_name, &nport) < 0) {
        return -1;
    }

    if (server_name == NULL) {
        server_name = default_server_name;
    }

    snprintf(port, 15, "%d", nport);

    // Connect to the server
    int client_socket;
    struct addrinfo hints, *servinfo, *p;
    int rv;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    if ((rv = getaddrinfo(server_name, port, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        exit(1);
    }

    for (p=servinfo; p != NULL; p = p->ai_next) {
        if ((client_socket = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
#ifdef DEBUG
            perror("socket");
#endif
            continue;
        }

        if (connect(client_socket, p->ai_addr, p->ai_addrlen) == -1) {
            close(client_socket);
#ifdef DEBUG
            perror("connect");
#endif
            continue;
        }

        // Connected succesfully!
        break;
    }

    if (p == NULL) {
        // The loop wasn't able to connect to the server
        fprintf(stderr, "Couldn't connect to the server\n.");
        exit(1);
    }
Run Code Online (Sandbox Code Playgroud)

而服务器:

    int nport;
    char port[15];
    if (parse_options(argc, argv, &nport) < 0) {
        return -1;
    }

    snprintf(port, 15, "%d", nport);

    int server_socket;
    struct addrinfo hints, *servinfo, *p;
    int rv;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;

    if ((rv = getaddrinfo(NULL, port, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        exit(1);
    }

    for (p=servinfo; p != NULL; p = p->ai_next) {
        if ((server_socket = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
#ifdef DEBUG
            perror("socket");
#endif
            continue;
        }

        if (bind(server_socket, p->ai_addr, p->ai_addrlen) == -1) {
            close(server_socket);
#ifdef DEBUG
            perror("bind");
#endif
            continue;
        }

        // We binded successfully!
        break;
    }

    if (p == NULL) {
        fprintf(stderr, "failed to bind socket\n");
        exit(2);
    }

    int pl_one, pl_two;
    socklen_t pl_one_len, pl_two_len;
    struct sockaddr_in pl_one_addr, pl_two_addr;

    if (listen(server_socket, 2) < 0) {
        fatal_error("Error in syscall listen.", 2);
    }

    // Get the two clients connections.
    pl_one_len = sizeof(pl_one_addr);
    pl_one = accept(server_socket,
                    (struct sockaddr *)&pl_one_addr,
                    &pl_one_len);
    if (pl_one < 0) {
        fatal_error("Error in syscall accept.", 3);
    }

    pl_two_len = sizeof(pl_two_addr);
    pl_two = accept(server_socket,
                    (struct sockaddr *)&pl_two_addr,
                    &pl_two_len);
    if (pl_two < 0) {
        fatal_error("Error in syscall accept.", 3);
    }
Run Code Online (Sandbox Code Playgroud)

如果我在命令行中指定了我的机器的IP,因此server_name在客户端中将其设置为类似的字符串151.51.xxx.xxx,则套接字无法连接到服务器.另外使用127.0.0.1显示相同的行为,这让我认为当文档说明:

您感兴趣的主机名在nodename参数中.地址可以是主机名,如"www.example.com",也可以是IPv4或IPv6地址(以字符串形式传递).

这只是在开玩笑.

我做错了什么吗?防火墙等有可能阻止客户端使用IP地址连接吗?

注:我已经搜查了很多关于这个问题,有些人说要避免使用getaddrinfo在所有与直接填写INADDR_ANY,但getaddrinfo文档指出传递NULLnodename应该已经装满了地址INADDR_ANY,所以我不明白为什么我应该使用新方法自动执行此操作时的旧方法.

typ*_*232 2

正如评论中所写,您正在尝试连接到位于路由器后面的网络中的服务器。

127.0.0.1或者localhost是操作系统的重定向,不会通过路由器。这就是为什么它对你有用。

如果指定外部 IP 地址,则通过路由器进行连接,并且需要在路由器配置中转发您使用的端口。默认情况下,任何普通的家庭互联网最终用户路由器都会阻止来自任何端口的传入连接。