简体   繁体   English

.net_ntop 始终返回相同的 IP

[英]inet_ntop always returns the same IP

Spending way too much time trying to figure out why .net_ntop is always returning the same IP address of 2.0.19.86 inside of my barebones C UDP socket program.花了太多时间试图弄清楚为什么.net_ntop总是在我的准系统 C UDP 套接字程序中返回2.0.19.86的相同 IP 地址。

Here is the code:这是代码:


#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;
}

The lines I am particularly stuck on is:我特别坚持的路线是:

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

For example I run the program with ./client www.google.com hello and get the following:例如,我使用./client www.google.com hello运行程序并得到以下信息:

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

I run the program again with ./client localhost hello and .net_ntop still returns the same IP.我用./client localhost hello再次运行程序, .net_ntop仍然返回相同的 IP。

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

No errors are being thrown when I am creating the socket, and the message sends successfully when I send it to the receiving program over localhost, why is .net_ntop still outputting this weird address?当我创建套接字时没有抛出任何错误,当我通过本地主机将消息发送到接收程序时消息发送成功,为什么 .net_ntop 仍然输出这个奇怪的地址?

In your call to .net_ntop :在您致电.net_ntop

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

You're not passing in the correct structure.您没有传递正确的结构。 When AF_.NET is passed as the first argument, the second argument should have type struct in_addr * , not struct sockaddr_in * .AF_.NET作为第一个参数传递时,第二个参数的类型应该是struct in_addr * ,而不是struct sockaddr_in *

You need to call out the sin_addr member which is of this type.需要调出该类型的sin_addr成员。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM