inet_addr 将字符串形式的IP地址 -> 网络字节顺序 的整型值
inet_ntoa 网络字节顺序的整型值 ->字符串形式的IP地址
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include
#include
using namespace std;
#pragma comment(lib, "ws2_32.lib")
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(1, 1);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
return 0;
}
char ip1[] = "192.168.0.74";
char ip2[] = "211.100.21.179";
struct in_addr addr1, addr2;
long l1, l2;
l1 = inet_addr(ip1); //将字符串形式的IP地址 -> 网络字节顺序 的整型值
l2 = inet_addr(ip2);
printf("IP1: %s\n IP2: %s\n", ip1, ip2);
printf("Addr1: %ld\n Addr2: %ld\n", l1, l2);
memcpy(&addr1, &l1, 4); //复制4个字节大小
memcpy(&addr2, &l2, 4);
printf("%s <--> %s\n", inet_ntoa(addr1), inet_ntoa(addr2)); //注意:printf函数自右向左求值、覆盖
printf("%s\n", inet_ntoa(addr1)); //网络字节顺序的整型值 ->字符串形式的IP地址
printf("%s\n", inet_ntoa(addr2));
getchar();
return 0;
}