C语言函数实现inet_addr
时间: 2023-05-16 12:07:01 浏览: 231
udp.rar_UDP c语言_UDP C语言_UDP 通信程序_c语言UDP
inet_addr() 函数用于将一个点分十进制的 IP 地址转换成一个 32 位的网络字节序的整数。该函数的实现如下:
unsigned long inet_addr(const char *cp)
{
int a, b, c, d;
if (sscanf(cp, "%d.%d.%d.%d", &a, &b, &c, &d) != 4) {
return INADDR_NONE;
}
if (a < 0 || a > 255 || b < 0 || b > 255 || c < 0 || c > 255 || d < 0 || d > 255) {
return INADDR_NONE;
}
return ((unsigned long)a << 24) | ((unsigned long)b << 16) | ((unsigned long)c << 8) | (unsigned long)d;
}
阅读全文