上一篇讲了一个简单的网络程序,其中用到了几个地址转换函数,今天这一篇来说说这几个函数的一些用法,
链接一://TODO
链接二://TODO
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int inet_aton(const char *cp, struct in_addr *inp);
in_addr_t inet_addr(const char *cp);
char *inet_ntoa(struct in_addr in);
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
//将点分十进制的字符串IP地址转换为32位数字(并且会自动将主机序转为网络序列),第一个参数cp为字符串,第二个参数inp为输出型参数,表示要更新的addr结构
int inet_aton(const char *cp, struct in_addr *inp);
//将点分十进制的字符串IP地址转换为32位数字
in_addr_t inet_addr(const char *cp);
//将点分十进制的字符串IP地址转化为32数字(并且会自动将主机序转为网络序列),并更新到dst里,第一个参数为协议类型,第三个参数为输出型参数,类型为void 它既可以转换IPv4和IPv6
int inet_pton(int af, const char *src, void *dst);
//将数字的IP地址转为点分十进制的字符串IP地址格式
char *inet_ntoa(struct in_addr in);
char *inet_ntoa(struct in_addr in);
函数返回一个char *类型,那么这个字符串是存储在哪里的呢,是否需要我们自己进行释放呢?看到man 手册里这样说:
The inet_ntoa() function converts the Internet host address in, given in network byte order, to a string in IPv4 dotted-decimal notation. The string is returned in a statically allocated buffer, which subsequent calls will overwrite. |
这个函数的返回值的字符串是放在静态区存储的,并且当你调用多次这个函数,结果是会被覆盖的。这样的话,在多线程环境中,这个函数是不安全的。
测试代码如下:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
void * func1(void *arg)
{
while(1)
{
sockaddr_in *addr=(sockaddr_in *)arg;
char * str1=inet_ntoa(addr->sin_addr);
printf(" expect 0.0.0.0 ,actual :%s\n",str1);
sleep(1);
}
}
void * func2(void *arg)
{
while(1)
{
sockaddr_in *addr=(sockaddr_in *)arg;
char * str2=inet_ntoa(addr->sin_addr);
printf(" expect 255.255.255.255 actual :%s\n",str2);
sleep(1);
}
}
int main()
{
pthread_t pthread_1,pthread_2;
//构造sockaddr_in结构
sockaddr_in addr1;
sockaddr_in addr2;
addr1.sin_addr.s_addr=0;
addr2.sin_addr.s_addr=0xffffffff;
//创建线程
pthread_create(&pthread_1,0,func1,&addr1);
pthread_create(&pthread_2,0,func2,&addr2);
//线程等待
pthread_join(pthread_1,NULL);
pthread_join(pthread_2,NULL);
return 0;
}
这里因为我的是Sentos 7,这里并没有得到想要的结果,希望大家可以自行测试,来进行验证是否是如上所说。 |
因为inet_ntoa()函数存在这样的问题,所以多线程环境中我们尽量避免使用这个函数,可以用inet_ntop()这个函数来代替。这个函数是提供了一个缓冲区来保存,不会出现上面的问题。
完。