本文共 942 字,大约阅读时间需要 3 分钟。
功能: 将一个IP转换成一个互联网标准点分格式的字符串。 原型: char FAR * inet_ntoa( struct in_addr in); 头文件:arpa/inet.h
inet_ntoa()返回的字符串是临时装在一个静态分配的缓冲区里面,下一次调用此函数的时候缓冲区会被重写
源码如下:
char *inet_ntoa (struct in_addr in){ __libc_once_define (static, once); char *buffer; unsigned char *bytes; /* If we have not yet initialized the buffer do it now. */ __libc_once (once, init); if (static_buf != NULL) buffer = static_buf; else { /* We don't use the static buffer and so we have a key. Use it to get the thread-specific buffer. */ buffer = __libc_getspecific (key); if (buffer == NULL) { /* No buffer allocated so far. */ buffer = malloc (18); if (buffer == NULL) /* No more memory available. We use the static buffer. */ buffer = local_buf; else __libc_setspecific (key, buffer); } } bytes = (unsigned char *) ∈ __snprintf (buffer, 18, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3]); return buffer;}
转载地址:http://utcci.baihongyu.com/