欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

socket()编成下inet_ntoa() and inet_addr()函数的使用

程序员文章站 2022-06-16 23:41:03
...

这是参考程序的网址
https://www.csdn.net/a4150902/article/details/7419995
C 库函数 - memcpy()
https://www.runoob.com/cprogramming/c-function-memcpy.html
自己实现:
#include<stdio.h>
#include<arpa/inet.h>

void typeorder()
{
union
{
short value;
char array[sizeof(short)];
}test;
test.value=0x0102;
if(test.array[0]==1)
printf(“host is 大端\n”);
else
printf(“host is little duan\n”);

}

int main()
{
typeorder();
//char * value1 = inet_aton(“192.168.0.102”,);
struct in_addr addr1,addr2;
unsigned int l1,l2;
l1=inet_addr(“192.168.0.74”);
l2=inet_addr(“211.56.65.32”);

    printf("l1=%d\n",l1);
    memcpy(&addr1,&l1,4);
    memcpy(&addr2,&l2,4);
    char * value1 = inet_ntoa(addr1);
    char * value2 = inet_ntoa(addr2);

    printf("1:%s\n",value1);
    printf("2:%s\n",value2);
    return 0;

}
实现结果
[email protected]:~$ ./test1
host is little duan
l1=1241557184
1:211.56.65.32
2:211.56.65.32