错误的输出 inet_ntop

jeh*_*tyy 4 c network-programming inet-ntop getaddrinfo

我正在尝试从 inet_ntop 打印一个 ip 地址,但输出似乎很奇怪。

该程序似乎运行良好,我成功连接了套接字,但它打印了以下内容:

H??H9?u?H?[]A\A]A^A_?ff.?
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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

int main(int argc, char *argv[]){
    int sock = socket(AF_INET, SOCK_STREAM, 0); 
    struct addrinfo hints, *result;
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = 0;
    hints.ai_flags = 0;

    int s = getaddrinfo("irc.root-me.org", "6667", &hints, &result);
    if( s != 0){ 
        printf("erreur\n");
        exit(EXIT_FAILURE);
    }   

    int f = connect(sock, result->ai_addr, result->ai_addrlen);
    if(f != 0){ 
        printf("erreur connect\n");
    }   

    struct sockaddr_in *sockin;  
    sockin = (struct sockaddr_in *)result->ai_addr;
    char  *dst;
    inet_ntop(AF_INET, &sockin->sin_addr, dst, sizeof(char *));
    printf("%s\n", dst);
    freeaddrinfo(result);

    exit(0);
}
Run Code Online (Sandbox Code Playgroud)

alk*_*alk 7

这里有两个问题:

  1. 3 RD参数inet_ntop()应该是一个char-array或一个指针1个ST一个的元件char-array。

  2. 第四参数 toinet_ntop()应该是目标缓冲区的大小,其地址作为第三个参数传递。

您要做的是将未初始化的char-pointerdst作为目标传递,并告诉函数它将指向sizeof(char*)字节内存。

一个AF_INET地址(该地址的形式xxx.xxx.xxx.xxx)使用最大的4x3的字符加3个限界. charS的总计为15个char小号加1附加char用作0终止子,使之成为C-“串”,所以校正后的代码看起来是这样的:

char dst[16] = ""; /* inet_ntop() does not necessarily 0-terminates the result. */
inet_ntop(AF_INET, &sockin->sin_addr, dst, sizeof dst);
Run Code Online (Sandbox Code Playgroud)

正如Filipe Gonçalves他的评论中指出的那样,可以使用INET_ADDRSTRLEN(如果可用)代替硬编码的“幻数”16来定义 IPv4 地址的文本表示的缓冲区大小。这是一件好事。


文档在inet_ntop()这里:

  • 很好的答案,但我建议使用“INET_ADDRSTRLEN+1”(在“arpa/inet.h”中定义)作为缓冲区的大小(如果使用 IPv6,则使用“INET6_ADDRSTRLEN+1”)。 (2认同)