inet_XX族函数
在网络编程中, 经常会将网络字节转为本地字节或者将本地字节转为网络字节, 但是如果每次我们都是都通过htonl
, ntohl
函数需要将10进制转为整数, 甚至还用将字符串转为整数, 再转为网络字节, 或者反过来都是很麻烦的. 还好linux都是提供很方便的函数让两者之间进行转换.
转换函数
linux提供了多种函数满足我们任何转换的需求, 这都是inet_xxx
族系列
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
int inet_aton(const char *ip, struct in_addr *inp); // 本地转网络
char * inet_ntoa(struct in_addr inp); // 网络转本地
in_addr_t inet_addr(const char *ip); // 返回网络字节
in_addr_t inet_network(const char *ip); // 返回本地字节
接下来我们来在分析一下这几个函数
1. inet_aton函数
将点分十进制IP转化为网络字节序存放在inp中, 并返回该网络字节序对应的整数.
int inet_aton(const char *ip, struct in_addr *inp);
失败 : 返回0.
成功 : 返回IP对应的网络字节序的数.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
// 传入 127.0.0.1
int main(int argc, char *argv[])
{
if(argc != 2)
exit(-1);
struct in_addr addr;
if(inet_aton(argv[1], &addr) == 0)
exit(-1);
printf("0x%08x\n", addr.s_addr); // 0x0100007f
exit(EXIT_SUCCESS);
}
2. inet_ntoa函数
将一个32位网络字节序的二进制IP地址转换成相应的点分十进制的IP地址.
char *inet_ntoa(struct in_addr inp);
失败 : 返回NULL.
成功 : 返回字符串指针.
注意 : 该函数不是线程安全函数, 不可重入. 因为不同线程调用会覆盖掉其他线程的缓冲区.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(int argc, char *argv[])
{
struct in_addr net_addr;
net_addr.s_addr = 0x0100007f; // 127.0.0.1
char *host_addr;
host_addr = inet_ntoa(net_addr);
printf("%s\n", host_addr); // 127.0.0.1
exit(EXIT_SUCCESS);
}
3. inet_addr函数
将一个点分十进制转换网络字节序IP.
in_addr_t inet_addr(const char *ip);
失败 : 返回INADDR_NONE. 部分手册上返回的是-1, 这个问题与inet_network
一样了.
成功 : 返回网络字节IP.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
// 输入 127.0.0.1
int main(int argc, char *argv[])
{
in_addr_t addr;
addr = inet_addr(argv[1]);
printf("0x%08x\n", addr); // 0x0100007f
exit(EXIT_SUCCESS);
}
4. inet_network函数
将点分十进制IP转化为主机字节序
in_addr_t inet_network(const char *ip);
失败 : 返回-1.
成功 : 返回主机对应的数.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
// 输入 127.0.0.1
int main(int argc, char *argv[])
{
in_addr_t addr;
addr = inet_network(argv[1]);
printf("0x%08x\n", addr); // 0x7f000001
exit(EXIT_SUCCESS);
}
该函数在某些情况下并不正确, 如果IP地址是255.255.255.255
返回的则是0xffffffff
, 转为十进制就是-1
. 分不清出-1
表示的是错误的返回值还是IP地址. 可以使用inet_pton()
和inet_ntop()
函数来代替, 这里就不详细的分析了, 有兴趣的可以自己查一下.
总结
本节分析了4个函数, 重点需要掌握的是inet_aton
函数.
- 4个函数使用, 以及返回值
inet_network
返回值的问题inet_ntoa
并不是线程安全的函数
inet_XX族函数的更多相关文章
- Linux学习笔记(8)-exec族函数
昨天学习了Linux下的进程创建,创建一个进程的方法极为简单,只需要调用fork函数就可以创建出一个进程,但是-- 介绍fork()函数的时候提到,在创建进程后,子进程与父进程有相同的代码空间,执行的 ...
- R-- Apply族函数
APPLY族函数: apply(x,a,f) 对矩阵或数据框的某一维度作用函数fx为矩阵或数据框:a为1代表行,a为2代表列:f为作用函数. lapply(x,f) 对x的每一个元组作用函数f,结果以 ...
- exec族函数详解及循环创建子进程
前言:之前也知道exec族函数,但没有完全掌握,昨天又重新学习了一遍,基本完全掌握了,还有一些父子进程和循环创建子进程的问题,还要介绍一下环境变量,今天分享一下. 一.环境变量 先介绍下环境的概念和特 ...
- 【Linux 进程】exec族函数详解
exec族的组成: 在Linux中,并不存在一个exec()的函数形式,exec指的是一组函数,一共有6个,分别是: #include <unistd.h> extern char **e ...
- R中的apply族函数和多线程计算
一.apply族函数 1.apply 应用于矩阵和数组 # apply # 1代表行,2代表列 # create a matrix of 10 rows x 2 columns m <- ma ...
- 从0开始自己用C语言写个shell__01_整体的框架以及fork和exec族函数的理解
最近才忙完了一个操作系统的作业,让我们用C语言实现一个Shell.总的来说,其实就是让我们 对系统调用有比较深的了解. 首先 介绍一下我的Shell 所实现的功能.1.运行可执行程序 即输入某个 标志 ...
- Linux-exec族函数
1.为什么需要exec族函数 (1).fork子进程是为了执行新程序(fork创建子进程后,子进程和父进程同时被OS调度执行,因此子程序可以单独的执行一个程序,这样程序宏观上将会和父进程程序同时进行) ...
- Linux exec族函数解析
背景 在提到 vfork 函数时,我们提到了这个概念.为了更好地学习与运用,我们对exec族函数进行展开. exec函数族 介绍 有时我们希望子进程去执行另外的程序,exec函数族就提供了一个在进程中 ...
- vector族函数
本文原创,转载请注明出处,本人Q1273314690 vector(mode = "logical", length = 0) as.vector(x, mode = " ...
随机推荐
- python多线程实现抓取网页
Python实现抓取网页 以下的Python抓取网页的程序比較0基础.仅仅能抓取第一页的url所属的页面,仅仅要预定URL足够多.保证你抓取的网页是无限级别的哈,以下是代码: ##coding:utf ...
- java中inputstream的使用
java中的inputstream是一个面向字节的流抽象类,其依据详细应用派生出各种详细的类. 比方FileInputStream就是继承于InputStream,专门用来读取文件流的对象,其详细继承 ...
- 某Android手游的lua源码逆向分析与还原
近日分析某一款Android上面的手游,反编译后再起asset目录下可以看到加密过的脚本,lib目录下发现lua的so 初步怀疑其使用lua脚本实现的 解密函数定位 动态跟踪解密函数流程 静态分析解密 ...
- Spring MVC学习-------------訪问到静态的文件
怎样訪问到静态的文件,如jpg,js,css? 怎样你的DispatcherServlet拦截"*.do"这种有后缀的URL.就不存在訪问不到静态资源的问题. 假设你的Dispat ...
- SQL SERVER:一条SQL语句插入多条记录等
在学习排名第二的mySql过程中,发现它的插入语句可以这样写: use test; create table fruits( fid char(10) not null ,s_id int null ...
- param size: The requested size, in points.
param size: The requested size, in points. 字幕宽度的自适应 . fontScale c++ - OpenCV find the text Scale fro ...
- 51nod 1611 金牌赛事
被亮亮D飞啦!!QWQ 这题明明是最大权闭合子图+线段树优化构图好不好 被迫考虑DP,并且考虑f[i]表示到第i个位置的最大值(第i个位置可选可不选) 对于最终的答案,我们可以分割成一段一段的,也就是 ...
- AngularJS2.0 一个表单例子——总体说来还是简化了1.x 使用起来比较自然
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- codeforces 920 EFG 题解合集 ( Educational Codeforces Round 37 )
E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 【POI 2010】 Antisymmetry
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2084 [算法] manacher [代码] #include<bits/std ...