完成到第三章3.4 P48

This commit is contained in:
riba2534
2019-01-13 14:11:31 +08:00
parent 40895a0ce5
commit 766d72b53f
4 changed files with 417 additions and 0 deletions

19
ch03/endian_conv.c Normal file
View File

@@ -0,0 +1,19 @@
#include <stdio.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
unsigned short host_port = 0x1234;
unsigned short net_port;
unsigned long host_addr = 0x12345678;
unsigned long net_addr;
net_port = htons(host_port); //转换为网络字节序
net_addr = htonl(host_addr);
printf("Host ordered port: %#x \n", host_port);
printf("Network ordered port: %#x \n", net_port);
printf("Host ordered address: %#lx \n", host_addr);
printf("Network ordered address: %#lx \n", net_addr);
return 0;
}