struct in_addr 结构体:

struct in_addr {in_addr_t s_addr;
};

表示一个32位的IPv4地址。

in_addr_t 一般为32位的unsigned int,其字节顺序为网络字节序,即该无符号数采用大端字节序。其中每8位表示一个IP地址中的一个数值。

打印的时候可以调用 inet_ntoa() 函数将其转换为char*类型。

头文件为:#include <arpa/inet.h>

inet——ntoa()函数用于将一个十进制网络字节序转换为点分十进制IP格式的字符串。

函数原型为:char*inet_ntoa(struct in_addr in);

网络字节序和主机字节序比较容易混乱(大端表示和小端表示)。

网络字节序采用大端表示,就是数据的高位要存放到低地址。

而大多数主机字节序采用小端表示(也有采用大端表示的主机字节序),就是数据的低位放到低地址。

比如无符号整型1338378,的二进制表示为:

数据的高位----------------------------》数据的地位

00000000 00010100 01101100 00001010

所以采用小端表示的主机字节序时,内存中存放的形式为:

低地址----------------------------------------》高地址

00001010 01101100 00010100 00000000

所以可以自己写个小程序来代替inet_ntoa()函数。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void toStringIP(const unsigned int ip,char *stringIP);
int main()
{unsigned int ip=1338378;char* stringIP = (char*)malloc(16);memset(stringIP,0,16+1);toStringIP(ip,stringIP);puts(stringIP);free(stringIP);return 0;
}void toStringIP(const unsigned int ip,char *stringIP)
{unsigned int tempIP=ip;for(int i=0;i<3;i++){unsigned char part=(char)tempIP;char temp[4];sprintf(temp,"%d.",part);strcat(stringIP,temp);tempIP=tempIP>>8;}unsigned char part=(char)tempIP;char temp[4];sprintf(temp,"%d",part);strcat(stringIP,temp);
}

参考
struct in_addr 结构体

struct in_addr 结构体相关推荐

  1. linux sock结构体,struct socket结构体详解

    在内核中为什么要有struct socket结构体呢? struct socket结构体的作用是什么? 下面这个图,我觉得可以回答以上两个问题.  由这个图可知,内核中的进程可以通过使用struct ...

  2. struct timeval结构体

    struct timeval结构体在time.h中的定义为: struct timeval { __time_t tv_sec;        /* Seconds. */ __suseconds_t ...

  3. C/C++结构体struct 与结构体数组和枚举型enum的结合使用

    C/C++结构体struct 与结构体数组和枚举型enum的结合使用 #include "stdafx.h" #include <string> #include &l ...

  4. struct ethhdr结构体详解

        在linux系统中,使用struct ethhdr结构体来表示以太网帧的头部.这个struct ethhdr结构体位于#include<linux/if_ether.h>之中. # ...

  5. struct timeval结构体 以及 gettimeofday()函数

    一.struct timeval结构体 struct timeval结构体在time.h中的定义为: struct timeval { __time_t tv_sec;        /* Secon ...

  6. struct device结构体(2.6.23)

    struct device结构体(2.6.23)   一.定义: linux/include/linux/device.h struct device {         struct klist   ...

  7. [matlab]使用struct创建结构体

    [matlab]使用struct创建结构体 语法: struct(域名1,属性值1,域名2,属性值2,.....) 我们具体看看结构体怎么使用: >> person(1)=struct(' ...

  8. struct sk_buff结构体详解

    struct sk_buff是linux网络系统中的核心结构体,linux网络中的所有数据包的封装以及解封装都是在这个结构体的基础上进行. 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  9. struct timeval结构体 以及 gettimeofday()函数、struct timespec结构体

    struct timeval结构体 struct timeval结构体在time.h中的定义为: struct timeval { __time_t tv_sec; /* Seconds. */ __ ...

最新文章

  1. Oracle归档日志删除
  2. 利用JS获取IE客户端IP及MAC的实现
  3. 对gridview中的一些操作。
  4. 轻松学习Linux之自动执行任务
  5. java500主键为空,java – JPA主键值始终为0
  6. 宣布降低Windows Azure Storage的定价
  7. 垃圾回收算法简单介绍——JVM读书笔记lt;二gt;
  8. 成员变量修饰词的作用
  9. java字符串是不是整数的函数_java判断字符串是否为整数的方法
  10. WPF 绘制对齐像素的清晰显示的线条
  11. C语言libcurl:提供特定主机和端口对的自定义地址--resolve
  12. numpy的增删改查操作
  13. java setundecorated_java JFrame中与setUndecorated()相关的几个特效
  14. Echarts异步获取数据不显示问题
  15. 我所理解的闭包是酱紫的
  16. 顺序表的初始化、插入、删除、查找
  17. 关于麒麟!关于互联网
  18. 7-2 输出数组元素分数 20
  19. 图灵奖得主(麦卡锡\霍尔)
  20. POSTGRESQL 差点发生一次冻结炸弹后的反思

热门文章

  1. 中国大学MOOC·Python网络爬虫与信息提取(一)
  2. 跳板攻击中如何追踪定位攻击者主机(上)
  3. 终端安全产品哪家强?
  4. rancid+CVS+cvsweb部署
  5. 若依 前后端分离调整主题色
  6. 微型计算机系统实验总结(学习性实验:IO地址译码,可编程并行接口8255,交通灯控制实验 + 自主设计实验:汽车信号灯控制系统,电风扇控制器,洗衣机控制系统,霓虹灯,电梯控制系统)
  7. RAMDISK 内存盘工具推荐
  8. 搜索引擎是如何最大化关键字广告收益的
  9. Centos最小化安装及配置
  10. 读书笔记—《销售铁军》随记2