Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

arpa/inet.h

  • 引用

    #include <arpa/inet.h>
    
    
    • 含有<netinet/in.h>的全部内容
  • ip地址转换

    in_addr_t inet_addr(const char *cp);
    
    • 把 点分10十进值表示法 转换成 32位整数
  • ip地址转换+填充

    int inet_aton(const char *cp, struct in_addr *addr)
    
    • 转换后,还能自动填充到addr指针代表的in_addr结构体,直接给sin_addr属性使用
    • 成功返回0,失败返回1
  • ip地址反向转换

    char * inet_ntoa(struct in_addr adr);
    
    • 提供in_addr结构体,会自动把里面的 32位整数 转换成 点分10十进值表示法,并存到内存空间返回指针
    • 返回后,要及时使用strcpy拷贝,否则再次调用时会覆盖之前的结果
  • 32位 主机字节序(大端或小端) 转 网络字节序(大端)

    uint32_t htonl(uint32_t hostlong);
    
    • 记忆: host to network long
    • 多用于ip地址转换
  • 16位 主机字节序(大端或小端) 转 网络字节序(大端)

    uint16_t htons(uint16_t hostshort);
    
    • 记忆: host to network short