网络编程:
1.思维导图:
2.上课写的代码:
2.1网络字节序与主机字节序转换
运行代码:
#include <myhead.h>
int main()
{
int num = 0x12345678;
short int value = 0x1234;
int num_n = htonl(num);
int value_n = htons(value);
printf("num_n = %#x,value_n = %#x\n",num_n,value_n);
return 0;
}
运行截图:
2.2验证大小端存储
运行代码:
#include<myhead.h>
int main(int argc, const char *argv[])
{
union{
int a;
char b;
}data;
data.a=0x12345678;
if(data.b==0x78){
printf("小端存储\n");
}else if(data.b==0x12){
printf("大端存储\n");
}
/*
int a=0x12345678;
char *b = (char *)&a;
if(0x78==*b){
printf("小段存储\n");
}else if(0x12==*b){
printf("大端存储\n");
}
*/
return 0;
}
运行截图:
2.3点分十进制转化网络字节序
运行代码:
#include<myhead.h>
int main(int argc, const char *argv[])
{
const char *ip = "192.168.12.82";
int ip_n=inet_addr(ip);
printf("ip_n=%#x\n",ip_n);
printf("主机字节序:%#x\n",ntohl(ip_n));
return 0;
}
运行截图: