ntohs, ntohl, htons,htonl inet_aton等詳解

來源:互聯網
上載者:User

標籤:des   style   blog   color   io   os   使用   ar   資料   

ntohs =net to host short int 16位htons=host to net short int 16位ntohs =net to host long int 32位htonl=host to net   long int   32位簡述:     將一個無符號短整形數從網路位元組順序轉換為主機位元組順序。     #include     u_short PASCAL FAR ntohs( u_short netshort);     netshort:一個以網路位元組順序表達的16位元。注釋:     本函數將一個16位元由網路位元組順序轉換為主機位元組順序。傳回值:     ntohs()返回一個以主機位元組順序表達的數。將主機的無符號短整形數轉換成網路位元組順序。#includeu_short PASCAL FAR htons( u_short hostshort);hostshort:主機位元組順序表達的16位元。注釋:本函數將一個16位元從主機位元組順序轉換成網路位元組順序。傳回值:htons()返回一個網路位元組順序的值。這2個函數提供了主機位元組順序與網路位元組順序的轉換比如網路位元組 為 00 01u_short     a;如何直接對應的話     a=0100; 為什麼呢?因為主機是從高位元組到低位元組的,所以應該轉化後a=ntohs(0001); 這樣 a=0001;首先,假設你已經有了一個sockaddr_in結構體ina,你有一個IP地址"132.241.5.10" 要儲存在其中,你就要用到函數inet_addr(),將IP地址從 點數格式轉換成無符號長整型。使用方法如下:ina.sin_addr.s_addr = inet_addr("132.241.5.10");; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 160%; ">注意,inet_addr()返回的地址已經是網路位元組格式,所以你無需再調用 函數htonl()。我們現在發現上面的代碼片斷不是十分完整的,因為它沒有錯誤檢查。 顯而易見,當inet_addr()發生錯誤時返回-1。記住這些位元字?(無符 號數)-1僅僅和IP地址255.255.255.255相符合!這可是廣播位址!大錯特 錯!記住要先進行錯誤檢查。好了,現在你可以將IP地址轉換成長整型了。有沒有其相反的方法呢? 它可以將一個in_addr結構體輸出成點數格式?這樣的話,你就要用到函數 inet_ntoa()("ntoa"的含義是"network to ascii"),就像這樣:printf("%s",inet_ntoa(ina.sin_addr));它將輸出IP地址。需要注意的是inet_ntoa()將結構體in-addr作為一 個參數,不是長整形。同樣需要注意的是它返回的是一個指向一個字元的 指標。它是一個由inet_ntoa()控制的靜態固定的指標,所以每次調用 inet_ntoa(),它就將覆蓋上次調用時所得的IP地址。例如:char *a1, *a2;..a1 = inet_ntoa(ina1.sin_addr);a2 = inet_ntoa(ina2.sin_addr);printf("address 1: %s ",a1);printf("address 2: %s ",a2);輸出如下:address 1: 132.241.5.10address 2: 132.241.5.10假如你需要儲存這個IP地址,使用strcopy()函數來指向你自己的字元指標。***********************************************************************************************************************************htonl()表示將32位的主機位元組順序轉化為32位的網路位元組順序 htons()表示將16位的主機位元組順序轉化為16位的網路位元組順序(ip地址是32位的連接埠號碼是16位的 )inet_ntoa()簡述:     將網路位址轉譯成“.”點隔的字串格式。     #include     char FAR* PASCAL FAR inet_ntoa( struct in_addr in);     in:一個表示Internet主機地址的結構。注釋:     本函數將一個用in參數所表示的Internet地址結構轉換成以“.” 間隔的諸如“a.b.c.d”的字串形式。請注意inet_ntoa()返回的字串存放在WINDOWS套介面實現所分配的記憶體中。應用程式不應假設 該記憶體是如何分配的。在同一個線程的下一個WINDOWS套介面調用前,資料將保證是有效。傳回值:     若無錯誤發生,inet_ntoa()返回一個字元指標。否則的話,返回NVLL。其中的資料應在下一個WINDOWS套介面調用前複製出來。參見:     inet_addr().測試代碼如下include#include#include#includep: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 160%; ">int main(int aargc, 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 ", inet_ntoa(addr1), inet_ntoa(addr2));     //注意這一句的運行結果         printf("%s ", inet_ntoa(addr1));         printf("%s ", inet_ntoa(addr2));         return 0;}實際運行結果如下:192.168.0.74 : 192.168.0.74       //從這裡可以看出,printf裡的inet_ntoa只運行了一次。192.168.0.74211.100.21.179inet_ntoa返回一個char *,而這個char *的空間是在inet_ntoa裡面靜態分配的,所以inet_ntoa後面的調用會覆蓋上一次的調用。第一句printf的結果只能說明在printf裡面的可變參數的求值是從右至左的,僅此而已。inet_aton,inet_addr和inet_ntoa在點分十進位數串(如,“192.168.1.10")與他的32位網路位元組二進位值之前轉換IPV4地址,有2個比較新的函數inet_pton和inet_ntop,這2個對IPV4和IPV6地址都能處理       #include <sys/socket.h>       #include <netinet/in.h>       #include <arpa/inet.h>       int inet_aton(const char *cp, struct in_addr *inp);       in_addr_t inet_addr(const char *cp);       char *inet_ntoa(struct in_addr in);inet_aton() converts the Internet host address cp from the standard       numbers-and-dots notation into binary data and stores it in the struc‐       ture that inp points to. inet_aton() returns non-zero if the address is       valid, zero if not.inet_aton() 轉換網路主機地址cp為位元值,並儲存在struct in_addr結構中,即第二個參數*inp,函數返回非0表示cp主機有地有效,返回0表示主機地址無效。The inet_addr() function converts the Internet host address cp from       numbers-and-dots notation into binary data in network byte order.   If       the input is invalid, INADDR_NONE (usually -1) is returned. This is an       obsolete interface to inet_aton(), described immediately above; it is       obsolete   because   -1 is a valid address (255.255.255.255), and       inet_aton() provides a cleaner way to indicate error return.inet_addr函數轉換網路主機地址(如192.168.1.10)為網路位元組序二進位值,如果參數char *cp無效,函數返回-1(INADDR_NONE),這個函數在處理地址為255.255.255.255時也返回- 1,255.255.255.255是一個有效地址,不過inet_addr無法處理;The inet_ntoa() function converts the Internet host address in given in       network byte order to a string in standard numbers-and-dots notation.       The string is returned in a statically allocated buffer, which subse‐       quent calls will overwrite.inet_ntoa 函數轉換網路位元組排序的地址為標準的ASCII以點分開的地址,,該函數返回指向點分開的字串地址的指標,該字串的空間為靜態分配的,這意味著在第二次調用該函數時,上一次調用將會被重寫(複蓋),所以如果需要儲存該串最後複製出來自己管理!現在一般使用inet_aton和inet_ntoa來處理網路位元組和主機位元組之間的轉換;有兩個更新的函數inet_pton和inet_ntop這2個函數能夠處理ipv4和ipv6,原型如下#include <sys/types.h>#include <sys/socket.h>#include <arpa/inet.h>int inet_pton(int af, const char *src, void *dst);這個函數轉換字串到網路地址,第一個參數af是地址族,轉換後存在dst中inet_pton 是inet_addr的擴充,支援的多地址族有下列:AF_INET       src為指向字元型的地址,即ASCII的地址的首地址(ddd.ddd.ddd.ddd格式的),函數將該地址       轉換為in_addr的結構體,並複製在*dst中AF_INET6       src為指向IPV6的地址,,函數將該地址       轉換為in6_addr的結構體,並複製在*dst中 如果函數出錯將返回一個負值,並將errno設定為EAFNOSUPPORT,如果參數af指定的地址族和src格式不對,函數將返回0。函數inet_ntop進行相反的轉換原型如下#include <sys/types.h>#include <sys/socket.h>#include <arpa/inet.h>const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt);這個函數轉換網路二進位結構到ASCII類型的地址,參數的作用和上面相同,只是多了一個參數socklen_t cnt,他是所指向緩衝區dst的大小,避免溢出,如果緩衝區太小無法儲存地址的值,則返回一個null 指標,並將errno置為ENOSPC

 

ntohs, ntohl, htons,htonl inet_aton等詳解(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.