日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

【Linux网络编程】网络字节序和地址转换

發布時間:2024/4/24 linux 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Linux网络编程】网络字节序和地址转换 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

00. 目錄

文章目錄

    • 00. 目錄
    • 01. 主機序到網絡序轉換函數
    • 02. 網絡序到主機序轉換函數
    • 03. inet_pton函數
    • 04. inet_ntop函數
    • 05. 附錄

01. 主機序到網絡序轉換函數

相關函數

#include <arpa/inet.h> uint32_t htonl(uint32_t hostlong); uint16_t htons(uint16_t hostshort); uint32_t ntohl(uint32_t netlong); uint16_t ntohs(uint16_t netshort);h表示主機序 n表示網絡序 s表示short類型 l表示long類型

htonl函數

uint32_t htonl(uint32_t hostint32); 功能:將 32 位主機字節序數據轉換成網絡字節序數據 參數:hostint32:需要轉換的 32 位主機字節序數據,uint32_t 為 32 為無符號整型 返回值:成功:返回網絡字節序的值

htons函數

uint16_t htons(uint16_t hostint16); 功能:將 16 位主機字節序數據轉換成網絡字節序數據 參數:hostint16:需要轉換的 16 位主機字節序數據,uint16_t,unsigned short int 返回值:成功:返回網絡字節序的值

測試程序:

#include <stdio.h> #include <arpa/inet.h>int main(int argc, char *argv[]) {int a = 0x01020304;short int b = 0x0102;printf("htonl(0x%08x) = 0x%08x\n", a, htonl(a));printf("htons(0x%04x) = 0x%04x\n", b, htons(b));return 0; }

測試結果:

deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ gcc 1.c deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ ./a.out htonl(0x01020304) = 0x04030201 htons(0x0102) = 0x0201 deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$

02. 網絡序到主機序轉換函數

ntohl函數

uint32_t ntohl(uint32_t netint32); 功能:將 32 位網絡字節序數據轉換成主機字節序數據 參數:netint32:待轉換的 32 位網絡字節序數據,uint32_t,unsigned int 返回值:成功:返回主機字節序的值

ntohs函數

uint16_t ntohs(uint16_t netint16); 功能:將 16 位網絡字節序數據轉換成主機字節序數據 參數:netint16:待轉換的 16 位網絡字節序數據,uint16_t,unsigned short int 返回值:成功:返回主機字節序的值

測試代碼:

#include <stdio.h> #include <arpa/inet.h>int main(int argc, char *argv[]) {int a = 0x01020304;short int b = 0x0102;printf("ntohl(0x%08x) = 0x%08x\n", a, ntohl(a));printf("ntohs(0x%04x) = 0x%04x\n", b, ntohs(b));return 0; }

測試結果:

deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ gcc 1.c deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ ./a.out ntohl(0x01020304) = 0x04030201 ntohs(0x0102) = 0x0201 deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$

03. inet_pton函數

inet_pton函數

#include <arpa/inet.h> int inet_pton(int family, const char *strptr, void *addrptr); 功能:將點分十進制數串轉換成 32 位無符號整數 參數:family:協議族( AF_INET、AF_INET6、PF_PACKET 等 ),常用 AF_INETstrptr:點分十進制數串addrptr:32 位無符號整數的地址 返回值:成功返回1失敗返回其它

測試代碼:

#include <stdio.h> #include <arpa/inet.h> int main() {char ip_str[]="192.168.12.133";unsigned int ip_uint = 0;unsigned char *ip_p = NULL;inet_pton(AF_INET,ip_str,&ip_uint);printf("in_uint = %#x\n",ip_uint);ip_p = (char *)&ip_uint;printf("in_uint = %d,%d,%d,%d\n",*ip_p,*(ip_p+1),*(ip_p+2),*(ip_p+3));return 0; }

測試結果:

deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ gcc 1.c deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ ./a.out in_uint = 0x850ca8c0 in_uint = 192,168,12,133 deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$

04. inet_ntop函數

const char *inet_ntop( int family, const void *addrptr, char *strptr, size_t len ); 功能:將 32 位無符號整數轉換成點分十進制格式的字符串 參數:family:協議族( AF_INET、AF_INET6、PF_PACKET 等 ),常用 AF_INETaddrptr:32 位無符號整數strptr:點分十進制數串len:strptr 緩存區長度len 的宏定義#define INET_ADDRSTRLEN 16 // for ipv4#define INET6_ADDRSTRLEN 46 // for ipv6返回值:成功:則返回字符串的首地址失敗:返回 NULL

測試代碼:

#include <stdio.h> #include <arpa/inet.h> int main() {unsigned char ip[] = {192, 168, 12, 133};char ip_str[16] = "\0";inet_ntop(AF_INET,(unsigned int *)ip,ip_str,16);printf("ip_str = %s\n",ip_str);return 0; }

測試結果:

deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ gcc 1.c deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ ./a.out ip_str = 192.168.12.133 deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$

05. 附錄

總結

以上是生活随笔為你收集整理的【Linux网络编程】网络字节序和地址转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。