【Linux网络编程】网络字节序和地址转换
生活随笔
收集整理的這篇文章主要介紹了
【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网络编程】网络字节序和地址转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Linux网络编程】无连接和面向连接协
- 下一篇: linux 其他常用命令