网络字节序与主机字节序相互转换
/********ntohl与htonl其实是一样的没有区别*********************/
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ip[] = "192.168.1.110";
int r1, r2, r3;
r1 = inet_addr(ip);
r2 = ntohl(r1);
r3 = ntohl(r2);
printf("a to n 0x%x\n", r1);
printf("n to h 0x%x\n", r2);
printf("n to h 0x%x\n", r3);
}
运行结果:a to n 0x6e01a8c0 //调用任意函数都是将尾字节从头排列
n to h 0xc0a8016e
n to h 0x6e01a8c0
/******将字符串IP转化为网络字节序与主机字节序**************/
char ip[] = "192.168.0.74";
long r1, r2, r3; //long
r1 = inet_addr(ip); //返回网络字节序
r2 = inet_network(ip); //返回主机字节序
/**************************************************************/
/******************************代码****************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
int main()
{
char ip[] = "192.168.0.74";
long r1, r2, r3; //long
struct in_addr addr;
r1 = inet_addr(ip); //返回网络字节序
if(-1 == r1){
printf("inet_addr return -1\n");
}else{
printf("inet_addr ip: %ld\n", r1);
}
r2 = inet_network(ip); //返回主机字节序
if(-1 == r2){
printf("inet_addr return -1\n");
}else{
printf("inet_network ip: %ld\n", r2);
printf("inet_network ip: %ld\n", ntohl(r2)); //ntohl: 主机字节序 ——> 网络字节序
}
r3 = inet_aton(ip, &addr); //返回网络字节序
if(0 == r3){
printf("inet_aton return -1\n");
}else{
printf("inet_aton ip: %ld\n", addr.s_addr);
}
}
/***** 批量注释的一种方法 *****/
#if 0
r3 = inet_aton(ip, addr);
if(0 == r3){
printf("inet_aton return -1\n");
}else{
printf("inet_aton ip: %ld\n", ntohl(addr.s_addr));
}
#endif
return 0;
}
列子:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
int main()
{
char ip[] = "192.168.1.1";
char *host_ip;
char *net_ip;
long host, net, host_net, net_host;
//字符串转为主机字节序
host = inet_network(ip);
printf("host=%ld\n", host);
//字符串转为网络字节序
net = inet_addr(ip);
printf("net=%ld\n", net);
//主机字节序转为网络字节序
host_net = htonl(host);
printf("host_net=%ld\n", host_net);
//网络字节序转为主机字节序
net_host = ntohl(net);
printf("net_host=%ld\n", net_host);
//主机字节序转为字符串
struct in_addr tmp_host;
tmp_host.s_addr = inet_network(ip);
host_ip = inet_ntoa(tmp_host);
printf("host_ip=%s\n", host_ip);
//网络字节序转为字符串
struct in_addr tmp_net;
tmp_net.s_addr = inet_addr(ip);
printf("net_ip=%s\n", inet_ntoa(tmp_net));
}
/******************************************************/
struct sockaddr {
sa_family_t sa_family;
char sa_data[14];
}
struct sockaddr_in{
short sin_family;
unsigned short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
typedef struct in_addr{
unsigned long s_addr; // in_addr_t = unsigned long
};
struct in_addr {
in_addr_t s_addr;
};
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);
char *inet_ntoa(struct in_addr in);
struct in_addr inet_makeaddr(int net, int host);
in_addr_t inet_lnaof(struct in_addr in);
in_addr_t inet_netof(struct in_addr in);
/************************************************************/
/*****************************总结*****************************/
如果转换的对象都是整型则用hton和ntoh, 这两个函数只是名字有区别其余都一样,
可随意用,如果是对字符串IP进行转换可用inet_addr和inet_network以及
inet_aton和inet_ntoa函数。还有较新的转换函数inet_pton与inet_ntop。