zoukankan      html  css  js  c++  java
  • Linux中ip地址结构和ip地址的转换

     ip地址结构

    struct sockaddr_in 
    {
        sa_family_t    sin_family; /* address family: AF_INET */
        in_port_t      sin_port;   /* port in network byte order */
        struct in_addr sin_addr;   /* internet address */
    };
    
    /* Internet address. */
    struct in_addr 
    {
        uint32_t       s_addr;     /* address in network byte order */
    };

    点分十进制ip转换:

    #include <stdio.h>
    #include <string.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netinet/ip.h>
    #include <arpa/inet.h>
    
    int 
    main()
    {
        const char *ip_str = "192.168.1.1";
        //const char *ip_str = "255.255.255.255";
        //const char *ip_str = "0.0.0.0";
        struct in_addr in;
        memset(&in, 0, sizeof(struct in_addr));
        in.s_addr = inet_addr(ip_str);    
        printf("ip addr is:%u
    ", in.s_addr);
        printf("ip addr is:%s
    ", inet_ntoa(in));
        memset(&in, 0, sizeof(struct in_addr));
        inet_aton(ip_str, &in);
        printf("ip addr is:%s
    ", inet_ntoa(in));
        return 0;
    }

    结果:

    shelmean:[~]$ ./a.out 
    ip addr is:16885952
    ip addr is:192.168.1.1
    ip addr is:192.168.1.1
    

     

  • 相关阅读:
    JS 中 this 关键字详解
    Excel 文本函数
    Excel 日期和时间函数
    Excel引用和数学函数
    Excel-查找函数
    Excel-统计函数
    数据分析-业务知识
    Excel-逻辑函数
    Excel-基本操作
    电商数据分析总结
  • 原文地址:https://www.cnblogs.com/shelmean/p/10054809.html
Copyright © 2011-2022 走看看