struct in_addr 结构体
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 结构体相关推荐
- linux sock结构体,struct socket结构体详解
在内核中为什么要有struct socket结构体呢? struct socket结构体的作用是什么? 下面这个图,我觉得可以回答以上两个问题. 由这个图可知,内核中的进程可以通过使用struct ...
- struct timeval结构体
struct timeval结构体在time.h中的定义为: struct timeval { __time_t tv_sec; /* Seconds. */ __suseconds_t ...
- C/C++结构体struct 与结构体数组和枚举型enum的结合使用
C/C++结构体struct 与结构体数组和枚举型enum的结合使用 #include "stdafx.h" #include <string> #include &l ...
- struct ethhdr结构体详解
在linux系统中,使用struct ethhdr结构体来表示以太网帧的头部.这个struct ethhdr结构体位于#include<linux/if_ether.h>之中. # ...
- struct timeval结构体 以及 gettimeofday()函数
一.struct timeval结构体 struct timeval结构体在time.h中的定义为: struct timeval { __time_t tv_sec; /* Secon ...
- struct device结构体(2.6.23)
struct device结构体(2.6.23) 一.定义: linux/include/linux/device.h struct device { struct klist ...
- [matlab]使用struct创建结构体
[matlab]使用struct创建结构体 语法: struct(域名1,属性值1,域名2,属性值2,.....) 我们具体看看结构体怎么使用: >> person(1)=struct(' ...
- struct sk_buff结构体详解
struct sk_buff是linux网络系统中的核心结构体,linux网络中的所有数据包的封装以及解封装都是在这个结构体的基础上进行. 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
- struct timeval结构体 以及 gettimeofday()函数、struct timespec结构体
struct timeval结构体 struct timeval结构体在time.h中的定义为: struct timeval { __time_t tv_sec; /* Seconds. */ __ ...
最新文章
- poj3253 优先队列
- 【mmdetection2.0错误】——ModuleNotFoundError: No module named ‘mmdet‘
- 11、OAuth和OpenID服务
- torch tensor去掉1维_南昌清污机新价格表1_海腾水工
- 信息学奥赛一本通(1145:字符串p型编码)
- 深入剖析Redis RDN持久化机制
- mysql5.7.14启动教程_mysql5.7.14安装配置方法图文详细教程
- java 读取 文件 short_Java RandomAccessFile readShort()方法
- 前端简单h5播放器的制作
- VNC方式连接树莓派
- Access2007数据库下载地址与AccessHelper
- 如何一键查询物流信息并筛选出未签收单号
- 顺序表的类型定义与基本操作
- 【影像配准】配准之棋盘网格图(镶嵌图像)(附有 C++ 代码)
- 手机按键精灵学习 —— 基础知识
- vue基础1--脚手架的使用与vue基础指令
- 【玩转Linux】Linux安装宝塔面板
- LaTex 数学之矩阵
- 整合篇:零基础学习与使用ElasticSearch
- FFMpeg4.0相关结构体和函数