函数简介
linux下:
函数声明:char *inet_ntoa (struct
in_addr);
所在头文件:<arpa/inet.h>
//end linux 下
函数功能:将
网络地址转换成“.”点隔的字符串格式。
所需库: winsock.h
也可以使用:
头文件
|
Winsock2.h
|
lib
|
Ws2_32.lib
|
dll
|
Ws2_32.dll
|
即在程序开头写:
#include <WINSOCK2.h>
#pragma comment(lib,"WS2_32.LIB")
函数原型: char FAR* PASCAL FAR inet_ntoa( struct
in_addr in);
MSDN上本函数的原型描述为:unsigned long inet_addr( __in const char *cp);
in:一个表示Internet
主机地址的结构。
注释:
本函数将一个用in参数所表示的Internet地址结构转换成以“.” 间隔的诸如“a.b.c.d”的字符串形式。请注意inet_ntoa()返回的字符串存放在WINDOWS
套接口实现所分配的内存中。
应用程序不应假设该内存是如何分配的。在同一个线程的下一个WINDOWS
套接口调用前,数据将保证是有效。
返回值:
相关函数:
inet_addr().
编辑本段程序示例
程序示例1:
#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 <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]);
return 1;
}
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
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();
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[])
{
struct
in_addr addr1,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只运行了一次。
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;
}