inet_ntoa()網路位元組序IP轉化為點分十進位制IP
程式示例1:
下面這個例子來自MSDN:
#include <winsock2.h>
#include <stdio.h>
#include <windows.h>
#pragma comment(lib, "wininet.lib")
// link with Ws2_32.lib
int __cdecl main(int argc, char **argv)
{
//-----------------------------------------
// Declare and initialize variables
WSADATA wsaData;
int iResult;
unsigned long ulAddr = INADDR_NONE;
// Validate the parameters
if (argc != 2) {
printf("usage: %s \n", argv[0]);
printf(" inetaddr converts a string containing an\n");
printf(" IPv4 address in one of the supported formats\n");
printf(" to a unsigned long representing an IN_ADDR\n");
printf(" %s 192.168.16.34\n", argv[0]);
return 1;
}
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
//--------------------------------
// Callinet_addr(). If the call succeeds,
// the result variable will
ulAddr = inet_addr(argv[1]);
if ( ulAddr == INADDR_NONE ) {
printf("inet_addr failed and returned INADDR_NONE\n");
WSACleanup();
return 1;
}
if (ulAddr == INADDR_ANY) {
printf("inet_addr failed and returned INADDR_ANY\n");
WSACleanup();
return 1;
}
printf("inet_addr returned success\n");
// Retrieve each address and print out the hex bytes
// for(i=0, ptr= (Char*) &ulAddr; i < 4; i++, ptr++) {
WSACleanup();
return 0;
}
程式示例2:
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
int main(int argc, char *argv[])
{
structin_addraddr1,addr2;
ulong l1,l2;
l1= inet_addr("192.168.0.74");
l2 = inet_addr("211.100.21.179");
memcpy(&addr1, &l1, 4);
memcpy(&addr2, &l2, 4);
printf("%s : %s\n", inet_ntoa(addr1), inet_ntoa(addr2)); //注意這一句的執行結果
printf("%s\n", inet_ntoa(addr1));
printf("%s\n", inet_ntoa(addr2));
return 0;
}
實際執行結果如下:
192.168.0.74 : 192.168.0.74 //從這裡可以看出,printf裡的inet_ntoa(addr2)先於inet_ntoa(addr1)執行。
192.168.0.74
211.100.21.179
inet_ntoa返回一個char *,而這個char *的空間是在inet_ntoa裡面靜態分配的,所以inet_ntoa後面的呼叫會覆蓋上一次的呼叫。第一句printf的結果只能說明在printf裡面的可變引數的求值是從右到左的,僅此而已。
程式示例3:
將示例2修改一下,使其執行在VC++6.0下:
#include <stdio.h>
#include <string.h>
#include <WINSOCK2.h>
#pragma comment(lib,"WS2_32.LIB")
int main(int argc, char *argv[])
{
struct in_addr addr1,addr2;
u_long l1,l2;
l1 = inet_addr("192.168.0.74");
l2 = inet_addr("211.100.21.179");
memcpy(&addr1, &l1, 4);
memcpy(&addr2, &l2, 4);
printf("%s : %s\n", inet_ntoa(addr1), inet_ntoa(addr2));
printf("%s\n", inet_ntoa(addr1));
printf("%s\n", inet_ntoa(addr2));
return 0;
}
程式示例4:
將示例1修改一下,使其執行在VC2008下:
#include <winsock2.h>
#include <stdio.h>
#include <windows.h>
#pragma comment(lib,"WS2_32.lib")
#include <Windows.h>
// link with Ws2_32.lib
int __cdecl main(int argc, char **argv)
{
//-----------------------------------------
// Declare and initialize variables
WSADATA wsaData;
int iResult;
unsigned long ulAddr = INADDR_NONE;
// Validate the parameters
if (argc != 2)
{
printf("usage: %s <IPv4 address>\n", argv[0]);
printf(" inetaddr converts a string containing an\n");
printf(" IPv4 address in one of the supported formats\n");
printf(" to a unsigned long representing an IN_ADDR\n");
printf(" %s 192.168.16.34\n", argv[0]);system("pause");
return 1;
}// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0)
{
printf("WSAStartup failed: %d\n", iResult);system("pause");
return 1;
}//--------------------------------
// Call inet_addr(). If the call succeeds,
// the result variable will hold a IN_ADDR
ulAddr = inet_addr(argv[1]);
if ( ulAddr == INADDR_NONE )
{
printf("inet_addr failed and returned INADDR_NONE\n");
WSACleanup();system("pause");
return 1;
}
if (ulAddr == INADDR_ANY)
{
printf("inet_addr failed and returned INADDR_ANY\n");
WSACleanup();system("pause");
return 1;
}
printf("inet_addr returned success\n");
// Retrieve each address and print out the hex bytes
// for(i=0, ptr= (Char*) &ulAddr; i < 4; i++, ptr++) {
WSACleanup();
system("pause");
return 0;
}