天天看點

linux inet_ntoa函數

inet_ntoa  

目錄

1基本介紹
2英文原意
3基本要求
4程式設計舉例

1基本介紹

功能: 将一個IP轉換成一個網際網路标準點分格式的字元串。 原型: char FAR * inet_ntoa( struct in_addr  in); 頭檔案: arpa/inet.h Winsock2.h 參數: 一個網絡上的IP位址 傳回值: 如果正确,傳回一個字元 指針,指向一塊存儲着點分格式IP位址的靜态緩沖區(同一線程内共享此記憶體);錯誤,傳回NULL。

2英文原意

The  inet_ntoa function converts an (Ipv4) Internet network address into a string in Internet standard dotted format. char FAR * inet_ntoa( struct in_addr  in); Parameters in [in] Structure that represents an Internet host address. Return Values If no error occurs,  inet_ntoa returns a character pointer to a static buffer containing the text address in standard ".'' notation. Otherwise, it returns NULL. Remarks The  inet_ntoa function takes an Internet address structure specified by the  in parameter and returns an ASCII string representing the address in ".'' (dot) notation as in "a.b.c.d.'' The string returned by  inet_ntoa resides in memory that is allocated by Windows Sockets. The application should not make any assumptions about the way in which the memory is allocated. The data is guaranteed to be valid until the next Windows Sockets function call within the same thread—but no longer. Therefore, the data should be copied before another Windows Sockets call is made.

3基本要求

作業系統:Windows 2000 Professional 或更高版本 頭檔案:Winsock2.h 連結庫:Ws2_32.lib 參見:

4程式設計舉例

SOCKADDR_IN addrSrv; addrSrv.sin_addr.S_un.S_addr=inet_addr("127.0.0.1"); char recvBuf[100]; char tempBuf[100]; sprintf(tempBuf,"%s say: %s",inet_ntoa(addrSrv.sin_addr),recvBuf); //将sin_addr儲存的IP(127.0.0.1)轉換成字元串形式。

繼續閱讀