문제

doesn't inet_aton suppose to normalize the dot version of the internet address? why do I get different output values for the example below?

int main(){
    char USER_IP[16] = "192.168.002.025";
    char USER_IP2[16] = "192.168.2.25";
    struct sockaddr_in addr;
    struct sockaddr_in addr2;

    inet_aton(USER_IP2, &addr.sin_addr);
    inet_aton(USER_IP, &addr2.sin_addr);

    printf("addr.sin_addr:%lu\n", addr.sin_addr);
    printf("addr2.sin_addr:%lu\n", addr2.sin_addr);


    return 0;
}

output:

addr.sin_addr:419604672
addr2.sin_addr:352495808
도움이 되었습니까?

해결책

from the documentation

components of the dotted address can be specified in decimal, octal (with a leading 0), or >hexadecimal, with a leading 0X)

this means that

char USER_IP[16] = "192.168.002.025";

implies 192 168 2 (25 Octal == 21) and

char USER_IP2[16] = "192.168.2.25";

implies 192 168 2 25

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top