zoukankan      html  css  js  c++  java
  • 主机字节序和网络字节序转换

    为什么要转换?

    主机字节序:整数在内存中保存的顺序,不同的处理器对应不容的模式

    Little endian 将低序字节存储在起始地址

    Big endian    将高序字节存储在起始地址

    网络字节序:整数在网络中的发送顺序

    网络字节顺序是TCP/IP中规定好的一种数据表示格式,它与具体的CPU类型、操作系统等无关,从而可以保证数据在不同主机之间传输时能够被正确解释。

    网络字节顺序采用big endian排序方式

    htons 本地的无符号short型主机字节序转换为网络字节序

    htonl     本地的无符号long型主机字节序转化为网络字节序

    ntohs    网络字节序转换为本地的无符号short型主机字节序

    ntohl     网络字节序转换为 本地的无符号long型主机字节序

    inet_addr: 将一个点间隔地址转换成  struct in_addr

    inet_ntoa: 将网络字节序格式IP转换到字符串

    inet_aton: 将字符串转换到网络字节序格式IP

    inet_pton: 将点十分进制转换为网络字节序

    atoi: 将字符串转换为整型数

    在使用little endian的系统中      这些函数会把字节序进行转换 
    在使用big endian类型的系统中 这些函数会定义成空宏

    参考  http://blog.csdn.net/suwei19870312/article/details/5320831

      http://www.360doc.com/content/12/0222/10/54470_188560773.shtml

    函数使用需要注意的问题:

    IPv4套接口地址数据结构

    结构:

        struct sockaddr_in {

         short int sin_family;     //IPV4协议为AF_INET

         unsigned short int sin_port;   //16位端口号,网络字节序列

         struct in_addr sin_addr; 

       unsigned char sin_zero[8];    //备用域,为了和struct sockaddr字节数保持相同;

      };

        struct in_addr{

           in_addr_t       s_addr;//32位IP地址,网络字节序列

    } ;

    参考 https://my.oschina.net/SBaof/blog/477585

  • 相关阅读:
    容器编排之rancher
    ActiveMQ安装配置
    Ansible Playbook
    AnsibleTower
    Ansible Configuration file
    jenkins报错jdk1.8/jre/lib/amd64/libawt_xawt.so
    Nexus安装配置
    maven 国内可用的中央仓库 阿里云
    jenkins Master stays offline if low disk space
    win版tesseract安装
  • 原文地址:https://www.cnblogs.com/Deanboy/p/7531235.html
Copyright © 2011-2022 走看看