linux網路位址轉譯函數

來源:互聯網
上載者:User

在socket編程時,會出現指定了ip,連接埠號碼,卻連不上,這是因為網路地址跟本地主機地址的字元順序不一樣導致的。

在linux下提供了一些操作函數,如下:

unsigned long int htonl(unsigned long int hostlong);

用來將參數指定的32位hostlong 轉換成網路字元順序。

unsigned short int htons(unsigned short int hostshort);

用來將參數指定的16位hostshort轉換成網路字元順序。

unsigned long int inet_addr(const char *cp);

用來將參數cp所指的網路地址字串轉換成網路所使用的位元字。

網路地址字串是以數字和點組成的字串,例如:“163.13.132.68”。

int inet_aton(const char * cp,struct in_addr *inp);

用來將參數cp所指的網路地址字串轉換成網路使用的二進位的數字,然後存於參數inp所指的in_addr結構中。

char * inet_ntoa(struct in_addr in);

用來將參數in所指的網路二進位的數字轉換成網路地址,然後將指向此網路地址字串的指標返回

unsigned long inet_network(const char *addr);

這個函數需要一個在參數addr中包含一個點分隔的地址輸入字串。

傳回值是主機順序的IP地址的32位值

The inet_lnaof() function returns the local host address part of

the Internet address in. The local host address is returned in local host byte order.

The inet_netof() function returns the network number part of the Internet Address in.

The network number is returned in local host byte order.

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.