inet_ntoa(), inet_aton(), inet_addr
来源:互联网 发布:ubuntu owncloud 编辑:程序博客网 时间:2024/04/30 05:03
Convert IP addresses from a dots-and-number string to a struct in_addr and back
Prototypes
#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>// ALL THESE ARE DEPRECATED! Use inet_pton() or inet_ntop() instead!!char *inet_ntoa(struct in_addr in);int inet_aton(const char *cp, struct in_addr *inp);in_addr_t inet_addr(const char *cp);
Description
These functions are deprecated because they don't handle IPv6! Use inet_ntop() or inet_pton() instead! They are included here because they can still be found in the wild.
All of these functions convert from a struct in_addr (part of your struct sockaddr_in, most likely) to a string in dots-and-numbers format (e.g. "192.168.5.10") and vice-versa. If you have an IP address passed on the command line or something, this is the easiest way to get a struct in_addr to connect() to, or whatever. If you need more power, try some of the DNS functions likegethostbyname() or attempt a coup d'État in your local country.
The function inet_ntoa() converts a network address in a struct in_addr to a dots-and-numbers format string. The "n" in "ntoa" stands for network, and the "a" stands for ASCII for historical reasons (so it's "Network To ASCII"—the "toa" suffix has an analogous friend in the C library called atoi() which converts an ASCII string to an integer.)
The function inet_aton() is the opposite, converting from a dots-and-numbers string into a in_addr_t (which is the type of the fields_addr in your struct in_addr.)
Finally, the function inet_addr() is an older function that does basically the same thing as inet_aton(). It's theoretically deprecated, but you'll see it a lot and the police won't come get you if you use it.
Return Value
inet_aton() returns non-zero if the address is a valid one, and it returns zero if the address is invalid.
inet_ntoa() returns the dots-and-numbers string in a static buffer that is overwritten with each call to the function.
inet_addr() returns the address as an in_addr_t, or -1 if there's an error. (That is the same result as if you tried to convert the string"255.255.255.255", which is a valid IP address. This is why inet_aton() is better.)
Example
struct sockaddr_in antelope;char *some_addr;inet_aton("10.0.0.1", &antelope.sin_addr); // store IP in antelopesome_addr = inet_ntoa(antelope.sin_addr); // return the IPprintf("%s\n", some_addr); // prints "10.0.0.1"// and this call is the same as the inet_aton() call, above:antelope.sin_addr.s_addr = inet_addr("10.0.0.1");
- inet_ntoa、 inet_aton、inet_addr
- inet_ntoa(), inet_aton(), inet_addr
- inet_aton、inet_addr、inet_ntoa函数
- inet_ntoa、 inet_aton、inet_addr
- inet_ntoa、 inet_aton、inet_addr
- inet_ntoa、 inet_aton、inet_addr
- inet_ntoa、 inet_aton、inet_addr
- inet_ntoa、 inet_aton、inet_addr 详细介绍
- 区分inet_addr、inet_aton和inet_ntoa
- htonl(),htons(),inet_addr(),inet_ntoa(), inet_aton()函数
- inet_aton,inet_ntoa,inet_addr,inet_network的细微区别
- 网络字节转换inet_aton & inet_ntoa & inet_addr和inet_pton & inet_ntop
- 网络字节转换inet_aton & inet_ntoa & inet_addr和inet_pton & inet_ntop
- 网络字节转换inet_aton & inet_ntoa & inet_addr和inet_pton
- 网络字节转换inet_aton & inet_ntoa & inet_addr和inet_pton & inet_ntop
- 网络字节转换inet_aton & inet_ntoa & inet_addr和inet_pton & inet_ntop
- 网络字节转换inet_aton & inet_ntoa & inet_addr和inet_pton & inet_ntop
- 网络字节转换inet_aton & inet_ntoa & inet_addr和inet_pton & inet_ntop
- Android 取消标题栏方法汇总
- 深度探索c++对象模型学习笔记
- 关于字符编码,你所需要知道的
- zoj zju 2991 Flipping Burned Pancakes
- 啊哈C——学习3.3一起来找茬
- inet_ntoa(), inet_aton(), inet_addr
- 如何解决Android SDK中离线文档打开慢的问题
- 如何去掉UISearchBar背景框?
- UVa-Beginning Algorithm Contests
- SGU 390 Tickets(数位DP)
- java多线程之生产者消费者经典问题
- java 操作 vsftp 文件
- 什么是哈希码?
- SSH框架总结(框架分析+环境搭建+实例源码下载)