Linux and C[2]: What is the function of inet_aton or inet ntoa ?

本文介绍了inet_aton函数的工作原理,该函数用于将IPv4地址从点分十进制字符串形式转换为网络字节序的32位整数。通过示例演示了如何使用此函数,并解释了从大端到小端格式的转换过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

What ever :inet_aton function translate ip address presentation such as “192.168.0.1” to numeric number in host.That is to say ,this function will transfer a string like ip address into a numeric ip address.

Test :

int main(){
int sockfd = socket(AF_INET,SOCK_RAW,0);
    struct sockaddr_in server_sock_in;
    bzero(&server_sock_in,sizeof(struct sockaddr_in));
    server_sock_in.sin_family=AF_INET;

    inet_aton("14.17.42.40",&(server_sock_in.sin_addr));
    printf("\nThe host 14.17.42.40 after aton function:\n%X\n",server_sock_in.sin_addr.s_addr);
    }

and the test result is like that:

The host 14.17.42.40 after aton function:
282A110E

According to the test above ,we can see how the function inet_aton works:
1. first , this function translate ‘14.17.42.40’ to big-endian like 0x0E 11 2A 28
2. then , this function will transfer big-endian to little endian if your system is little endian.0x 0E 11 2A 28–>0x 28 2A 11 0E

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值