ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

网络序,主机序学习

2019-10-19 10:55:08  阅读:303  来源: 互联网

标签:addr 主机 0x 网络 192.168 unsigned 学习 1.100 inet


网络序,主机序学习

网络序,主机序总是迷迷糊糊的。需要整理一下。
千言万语,不如代码来的实在。哈哈,说干就干。

#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>

int host_order() 
{
    unsigned long a = 0x12345678;
    unsigned char * p = (unsigned char *)(&a);

    printf("主机字节序:%0x  %0x %0x %0x\n",p[0],p[1],p[2],p[3]);

    unsigned long b = htonl(a);

    p = (unsigned char *)(&b);

    printf("网络字节序:%0x  %0x %0x %0x\n",p[0],p[1],p[2],p[3]);

    return 0;
}

int net_order()
{
    struct in_addr ipaddr;
    unsigned long addr = inet_addr("192.168.1.100");
    unsigned char *p = (unsigned char *)(&addr);
    printf("addr = %u \n",addr);
    printf("%0x %0x %0x %0x : inet_addr 192.168.1.100\n",p[0],p[1],p[2],p[3]);
    printf("%d  %d  %d  %d : inet_addr 192.168.1.100\n",p[0],p[1],p[2],p[3]);
    unsigned long addr_host = ntohl(addr);
    p = (unsigned char *)(&addr_host);
    printf("%0x %0x %0x %0x : inet_addr ntohl 192.168.1.100\n",p[0],p[1],p[2],p[3]);
    printf("%d  %d  %d  %d : inet_addr ntohl 192.168.1.100\n",p[0],p[1],p[2],p[3]);
    printf("addr_host = %u\n",addr_host);
    return 0;
}

int main()
{
    int ret = 0;
    //ret = host_order();
    ret = net_order();
    return ret ;
}

编译之。

gcc -g  -o test_order test_order.c

跑一下。

[root@localhost test]# ./test_order                       
addr = 1677830336 
c0      a8 1    64 : inet_addr 192.168.1.100
192     168  1  100 : inet_addr 192.168.1.100
64      1 a8    c0 : inet_addr ntohl 192.168.1.100
100     1  168  192 : inet_addr ntohl 192.168.1.100
addr_host = 3232235876

清晰明了。有助于快速理解。

标签:addr,主机,0x,网络,192.168,unsigned,学习,1.100,inet
来源: https://blog.51cto.com/qiaopeng688/2443790

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有