inet_ntop总是returns同一个IP

inet_ntop always returns the same IP

花了太多时间试图弄清楚为什么 inet_ntop 在我的准系统 C UDP 套接字程序中总是返回与 2.0.19.86 相同的 IP 地址。

代码如下:


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define SERVERPORT "4950"    // the port users will be connecting to

int main(int argc, char *argv[])
{
    int sock;
    struct addrinfo addr_type, *server_info, *p;
    int err;
    int numbytes;
    

    if (argc != 3) {
        fprintf(stderr,"usage: talker hostname message\n");
        exit(1);
    }

    //Specify type of response we want to git
    memset(&addr_type, 0, sizeof addr_type);
    addr_type.ai_family = AF_INET; // set to AF_INET to use IPv4
    addr_type.ai_socktype = SOCK_DGRAM;


    //Get the address info (like IP address) and store in server_info struct
    if ((err = getaddrinfo(argv[1], SERVERPORT, &addr_type, &server_info)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(err));
        return 1;
    }

    // There might be multiple IP addresses...loop through and use the first one that works
    for(p = server_info; p != NULL; p = p->ai_next) {
        if ((sock = socket(p->ai_family, p->ai_socktype,
                p->ai_protocol)) == -1) {
            perror("Error when creating socket");
            continue;
        }

        break;
    }

    if (p == NULL) {
        fprintf(stderr, "Client failed to create socket\n");
        return 2;
    }
    
    char s[INET_ADDRSTRLEN];
    inet_ntop(AF_INET,(struct sockaddr_in *)p->ai_addr,s, sizeof s);
    printf("sending to %s....\n",s);

    if ((numbytes = sendto(sock, argv[2], strlen(argv[2]), 0,
             p->ai_addr, p->ai_addrlen)) == -1) {
        perror("Error sending message");
        exit(1);
    }


    printf("client sent %d bytes to %s\n", numbytes, argv[1]);


    freeaddrinfo(server_info);
    close(sock);

    return 0;
}

我特别卡的台词是:

    char s[INET_ADDRSTRLEN];
    inet_ntop(AF_INET,(struct sockaddr_in *)p->ai_addr,s, sizeof s);
    printf("sending to %s....\n",s);

例如,我 运行 带有 ./client www.google.com hello 的程序并得到以下内容:

sending to 2.0.19.86....
client sent 5 bytes to www.google.com

我再次 运行 使用 ./client localhost helloinet_ntop 的程序仍然 returns 相同的 IP。

sending to 2.0.19.86....
client sent 5 bytes to localhost

创建socket时没有报错,通过localhost发送到接收程序时消息发送成功,为什么inet_ntop仍然输出这个奇怪的地址?

在你给 inet_ntop 的通话中:

inet_ntop(AF_INET,(struct sockaddr_in *)p->ai_addr,s, sizeof s);

您没有传递正确的结构。当 AF_INET 作为第一个参数传递时,第二个参数的类型应该是 struct in_addr *,而不是 struct sockaddr_in *.

您需要调出属于该类型的sin_addr成员

inet_ntop(AF_INET, &((struct sockaddr_in *)p->ai_addr)->sin_addr, s, sizeof s);