1. 程式人生 > >【轉】inet_addr、inet_aton、inet_pton異同小結

【轉】inet_addr、inet_aton、inet_pton異同小結

知識背景:

210.25.132.181屬於IP地址的ASCII表示法,也就是字串形式。英語叫做IPv4 numbers-and-dots notation。

如果把210.25.132.181轉換為整數形式,是3524887733,這個就是整數形式的IP地址。英語叫做binary data。(其實binary是二進位制的意思)

問題所在:

如何在字串形式的IP和整數形式的IP之間轉換呢?

轉換函式:

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

in_addr_t inet_addr(const char *cp);

in_addr_t inet_network(const char *cp);

int inet_pton(int af, const char *src, void *dst);

const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt);

=============================================================

IPv4:

IP字串 ——》 網路位元組流

inet_addr、inet_network、inet_aton

程式程式碼:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <netinet/in.h>
  6. #include <sys/socket.h>
  7. #include <sys/types.h>
  8. int main()  
  9. {  
  10.     char ip[] = "192.168.0.74";  
  11.     long r1, r2, r3;  //long
  12.     struct in_addr addr;  
  13.     r1 = inet_addr(ip); //返回網路位元組序
  14.     if(-1 == r1){  
  15.         printf("inet_addr return -1/n");  
  16.     }else{  
  17.         printf("inet_addr ip: %ld/n", r1);  
  18.     }  
  19.     r2 = inet_network(ip);    //返回主機位元組序
  20.     if(-1 == r2){  
  21.         printf("inet_addr return -1/n");  
  22.     }else{  
  23.         printf("inet_network ip: %ld/n", r2);   
  24.         printf("inet_network ip: %ld/n", ntohl(r2));   //ntohl: 主機位元組序 ——> 網路位元組序
  25.     }  
  26.     r3 = inet_aton(ip, &addr);  //返回網路位元組序
  27.     if(0 == r3){  
  28.         printf("inet_aton return -1/n");  
  29.     }else{  
  30.         printf("inet_aton ip: %ld/n", addr.s_addr);  
  31.     }  
  32. /*****  批量註釋的一種方法  *****/
  33. #if 0  
  34.     r3 = inet_aton(ip, addr);  
  35.     if(0 == r3){  
  36.         printf("inet_aton return -1/n");  
  37.     }else{  
  38.         printf("inet_aton ip: %ld/n", ntohl(addr.s_addr));  
  39.     }  
  40. #endif
  41.     return 0;  
  42. }  

執行結果:

[[email protected] net]$ gcc -W -o inet_addr inet_addr.c 
[[email protected] net]$ ./inet_addr                     
inet_addr ip: 1241557184
inet_network ip: -1062731702
inet_network ip: 1241557184
inet_aton ip: 1241557184 

--------------------------------------------------------------------------

IP字串 《——》 網路位元組流

inet_addr、inet_aton、inet_ntoa

 程式程式碼:

  1. #include <stdio.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <string.h>
  6. int main(int argc, char *argv[])  
  7. {  
  8.     char ip1[] = "192.168.0.74";  
  9.     char ip2[] = "211.100.21.179";  
  10.     struct in_addr addr1, addr2;  
  11.     long l1, l2;  
  12.     l1 = inet_addr(ip1);   //IP字串——》網路位元組
  13.     l2 = inet_addr(ip2);  
  14.     printf("IP1: %s/nIP2: %s/n", ip1, ip2);  
  15.     printf("Addr1: %ld/nAddr2: %ld/n", l1, l2);  
  16.     memcpy(&addr1, &l1, 4); //複製4個位元組大小
  17.     memcpy(&addr2, &l2, 4);  
  18.     printf("%s <--> %s/n", inet_ntoa(addr1), inet_ntoa(addr2)); //注意:printf函式自右向左求值、覆蓋
  19.     printf("%s/n", inet_ntoa(addr1)); //網路位元組 ——》IP字串
  20.     printf("%s/n", inet_ntoa(addr2));  
  21.     return 0;  
  22. }  

執行結果:

[[email protected] net]$ gcc -W -o inet_ntoa inet_ntoa.c 

[[email protected] net]$ ./inet_ntoa                                      

IP1: 192.168.0.74

IP2: 211.100.21.179

Addr1: 1241557184

Addr2: 3004523731

192.168.0.74 <--> 192.168.0.74

192.168.0.74

211.100.21.179

============================================================= 

IPv6:

IPv4 字串 《——》網路位元組流

inet_pton、inet_ntop

程式程式碼:

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <arpa/inet.h>
  5. int main()  
  6. {  
  7.     char ip[] = "192.168.0.74";   
  8.     struct in_addr addr;  
  9.     int ret = inet_pton(AF_INET, ip, (void *)&addr);   //IP字串 ——》網路位元組流
  10.     if(0 == ret){  
  11.         printf("inet_pton error, return 0/n");  
  12.         return -1;  
  13.     }else{  
  14.         printf("inet_pton ip: %ld/n", addr.s_addr);  
  15.         printf("inet_pton ip: 0x%x/n", addr.s_addr);  
  16.     }  
  17.     constchar *pstr = inet_ntop(AF_INET, (void *)&addr, ip, 128);  //網路位元組流 ——》IP字串
  18.     if(NULL == pstr){  
  19.         printf("inet_ntop error, return NULL/n");  
  20.         return -1;  
  21.     }else{  
  22.         printf("inet_ntop ip: %s/n", ip);  
  23.     }  
  24.     return 0;  
  25. }  

執行結果:

[[email protected] net]$ gcc -W -o inet_ptoa inet_ptoa.c 
[[email protected] net]$ ./inet_ptoa                     
inet_pton ip: 1241557184
inet_pton ip: 0x4a00a8c0
inet_ntop ip: 192.168.0.74

--------------------------------------------------------------------------

IPv6 字串 《——》網路位元組流

inet_pton、inet_ntop

程式程式碼:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <arpa/inet.h>
  5. int
  6. main(int argc, char **argv)  
  7. {  
  8.     unsigned char buf[sizeof(struct in6_addr)];  
  9.     int domain, s;  
  10.     char str[INET6_ADDRSTRLEN];  
  11.     if(argc != 3){  
  12.         fprintf(stderr, "usage: %s {i4|i6|<num>} string/n", argv[0]);  
  13.         exit(EXIT_FAILURE);  
  14.     }  
  15.     domain = (strcmp(argv[1], "i4") == 0) ? AF_INET:(strcmp(argv[1], "i6") == 0) ? AF_INET6 : atoi(argv[1]);  
  16.     //IP字串 ——》網路位元組流
  17.     s = inet_pton(domain, argv[2], buf);  
  18.     if(s<=0){  
  19.         if(0 == s)  
  20.             fprintf(stderr, "Not in presentation format/n");  
  21.         else
  22.             perror("inet_pton");  
  23.         exit(EXIT_FAILURE);  
  24.     }  
  25.     //網路位元組流 ——》IP字串
  26.     if(inet_ntop(domain, buf, str, INET6_ADDRSTRLEN) == NULL){    
  27.         perror("inet ntop/n");  
  28.         exit(EXIT_FAILURE);  
  29.     }  
  30.     printf("%s/n", str);  
  31.     exit(EXIT_SUCCESS);  
  32. }  
執行結果:

[[email protected] net]$ gcc -W -o inet_ptoa6 inet_ptoa6.c                               
[[email protected] net]$ ./inet_ptoa6 i6 0:0:0:0:0:FFFF:204.152.189.116   
::ffff:204.152.189.116
[[email protected] net]$ ./inet_ptoa6 i4 204.152.189.116               
204.152.189.116

相關推薦

inet_addrinet_atoninet_pton異同小結

知識背景: 210.25.132.181屬於IP地址的ASCII表示法,也就是字串形式。英語叫做IPv4 numbers-and-dots notation。 如果把210.25.132.181轉換為整數形式,是3524887733,這個就是整數形式的IP地址。英語叫做binary data。(其實

shell expect spawnlinux expect 用法小記 看著舒服點

ssh username 英文 認識 exe 無法找到 usr 過去 操作 使用expect實現自動登錄的腳本,網上有很多,可是都沒有一個明白的說明,初學者一般都是照抄、收藏。可是為什麽要這麽寫卻不知其然。本文用一個最短的例子說明腳本的原理。 腳本代碼如下:  #####

QT中QWidgetQDialog及QMainWindow的區別

屏幕 編輯 派生 標記 裝飾 按鈕 set 沒有 idg QWidget類是所有用戶界面對象的基類。 窗口部件是用戶界面的一個基本單元:它從窗口系統接收鼠標、鍵盤和其它事件,並且在屏幕上繪制自己。每一個窗口部件都是矩形的,並且它們按Z軸順序排列。一個窗口部件可以被它的父窗口

開源許可證GPLBSDMITMozillaApache和LGPL的區別

2.0 源程序 組織 alt 要求 控制 知識產權 bsp script 首先借用有心人士的一張相當直觀清晰的圖來劃分各種協議:開源許可證GPL、BSD、MIT、Mozilla、Apache和LGPL的區別 以下是上述協議的簡單介紹:BSD開源協議BSD開源協議是一個

淺談JavaScriptES5ES6

javascrip clas create 支持 ssi 對象 關鍵字 操作符 cnblogs 本文轉自http://www.cnblogs.com/lovesong/p/4908871.html 什麽是JavaScript JavaScript一種動態類型、弱類型、基於原

C# Linq 交集並集差集去重

log .cn pre tin nio clas int except post 轉自: https://www.cnblogs.com/wdw31210/p/4167306.html using System.Linq; List<string&

adb.模擬點擊滑動輸入按鍵

按鍵 roi tps mut sta previous col ast ear 1、android adb 模擬點擊、滑動、輸入、按鍵 - 陽光檸檬_的技術筆記 - CSDN博客.html(https://blog.csdn.net/liukang325/article/d

adb.模擬點選滑動輸入按鍵

1、android adb 模擬點選、滑動、輸入、按鍵 - 陽光檸檬_的技術筆記 - CSDN部落格.html(https://blog.csdn.net/liukang325/article/details/79268173)   ZC:“模擬滑動” 2、 //模擬輸入“001” adb shell

python開發大全系列文章精品教程

python應用教程 python後臺架構Django教程 python自動化測試教程 python網路爬蟲教程

PHP:echoprintprint_rsprintfvar_dump之間的區別

- echo是命令,不能返回值。echo後面可以跟很多個引數,之間用分號隔開,如: echo $myvar1; echo 1,2,$myvar,"<b>bold</b>"; - print是函式,可以返回一個值,只能有一個引數。 - print

python開發大全系列文章精品教程

版權宣告:本文為博主原創文章,轉載請註明來源。開發合作聯絡[email protected] https://blog.csdn.net/luanpeng825485697/article/details/78347433 python基礎教程 python基

Java -- 偏向鎖輕量級鎖自旋鎖重量級鎖

之前做過一個測試,詳情見這篇文章《多執行緒 +1操作的幾種實現方式,及效率對比》,當時對這個測試結果很疑惑,反覆執行過多次,發現結果是一樣的:  1. 單執行緒下synchronized效率最高(當時感覺它的效率應該是最差才對);  2. AtomicInteger效率最不穩定,

Java靜態變數靜態常量靜態方法

Java靜態變數、靜態常量、靜態方法        靜態變數和靜態方法都屬於靜態物件。在外部呼叫靜態方法時,可以使用"類名.方法名"的方式,也可以使用"物件名.方法名"的方式。而例項方法只有後面這種方式。也就是說,呼叫靜態方法可以無需建立物件。 1、J

MongoDB 應用場景避坑事項與最佳實踐

  MongoDB 是一個高效能,開源,無模式的文件型資料庫,是當前 NoSQL 資料庫產品中最熱門的一種。它在許多場景下可用於替代傳統的關係型資料庫或鍵/值儲存方式,MongoDB 使用 C++開發。 為什麼要用 NoSQL NoSQL,全稱是”Not Only Sql”,指的

一些圖論網路流入門題總結彙總

最短路問題此類問題型別不多,變形較少 生成樹問題基本的生成樹就不放上來了 連通性,度數,拓撲問題此類問題主要牽扯到DFS,縮點等技巧 POJ 3352 - Road Const

Servlet 生命週期工作原理

Servlet 生命週期:Servlet 載入--->例項化--->服務--->銷燬。 init():在Servlet的生命週期中,僅執行一次init()方法。它是在伺服器裝入Servlet時執行的,負責初始化Servlet物件。可以配置伺服器,以在啟動伺服器或客戶機首次訪問Servlet

scikit-learn隨機森林調參小結

【轉】https://blog.csdn.net/xuxiatian/article/details/54410086 轉自:http://www.cnblogs.com/pinard/p/6160412.html 在Bagging與隨機森林演算法原理小結中,我們對隨機森林(Random

淺談一個網頁打開的全過程(涉及DNSCDNNginx負載均衡等)

位置 filters 產生 多種方法 tps windows cnblogs 這就是 廣東 1、概要   從用戶在瀏覽器輸入域名開始,到web頁面加載完畢,這是一個說復雜不復雜,說簡單不簡單的過程,下文暫且把這個過程稱作網頁加載過程。下面我將依靠自己的經驗,總結一下整個過程

常用牛人主頁鏈接(計算機視覺模式識別機器學習相關方向,陸續更新。。。。)

short psu works charles 貝葉斯 learning 數學 ocr 相關 轉自:http://blog.csdn.net/goodshot/article/details/53214935 目錄(?)[-] The Kalman

哈希(Hash)與加密(Encrypt)的基本原理區別及工程應用

phy 理論 靈活運用 十分 實際應用 廣泛 tle 多網站 net 0、摘要 今天看到吉日嘎拉的一篇關於管理軟件中信息加密和安全的文章,感覺非常有實際意義。文中作者從實踐經驗出發,討論了信息管理軟件中如何通過哈希和加密進行數據保護。但是從文章評論中也可以