附加说明下面为您的享受:
// This is the number of IP string buffers.
#define IPTOSBUFFERS 12
char *iptos(u_long in)
{
// 12 buffers, each big enough to hold maximum-sized IP address
// and nul terminator.
static char output[IPTOSBUFFERS][3*4+3+1];
// Last buffer used.
static short which;
// Get uns. char pointer to IP address.
u_char *p;
p = (u_char *)∈
// Move to next string buffer, wrapping if necessary.
which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1);
// Output IP address by accessing individual unsigned chars in it.
_snprintf_s(output[which], sizeof(output[which]), sizeof(output[which]),
"%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
// Return the current buffer.
return output[which];
}
它的工作原理是因为IPv4地址的表示是在存储器中的32位的值,并且每个所述四个区段的占据每一个八位字节。因此,将32位整数的地址转换为四字符数组然后使用该数组提取单个段是相对简单的事情。当然,这是根据具有特定位宽度的数据类型来预测的,因此它不是便携式的。
奇怪的是12-IP地址循环队列。也许这样可以一次获得多达12个IP地址,而不会覆盖字符串,尽管我认为我从未遇到过超过两个(可能是三个代理服务器或pass-thru服务器)的情况需要在同一时间。我不认为这是线程安全的,因为在线程环境中对which
的修改本质上是危险的。
你不了解什么?它看起来像是一个无符号的long,并且不幸的是它返回一个带有悬挂指针的ip字符串表示。 – WhirlWind 2010-05-05 15:51:57
'172.16.48.1'的u_long是什么? – user198729 2010-05-05 15:53:53
172 * 256^3 + 16 * 256^2 + 48 * 256^1 + 1 – WhirlWind 2010-05-05 15:56:11