#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/*
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);
*/
// cp 点分十进制字符串 如 198.0.0.1
/*
struct in_addr {
unsigned long int s_addr;
}
*/
#include<stdio.h>
#include<string.h>
int main()
{
const char *cp = "192.168.0.10";
struct in_addr in_addr_tmp;
//1. inet_aton
memset(&in_addr_tmp,0,sizeof in_addr_tmp);
if(inet_aton(cp,&in_addr_tmp) == 0)
{
printf("error\n");
exit(0);
}
printf("%ld\n",in_addr_tmp.s_addr);
//2. inet_addr
in_addr_t in_addr_t_1 = inet_addr(cp);
printf("%ld\n",in_addr_t_1);
printf("%ld\n",htonl(in_addr_t_1));
//3. inet_network
printf("cp=%s\n",cp);
in_addr_t in_addr_t_2 = inet_network(cp);
printf("%ld\n",in_addr_t_2);
printf("%ld\n",ntohl(in_addr_t_2));
//4. inet_ntoa
printf("%s\n",inet_ntoa(in_addr_tmp));
//5.net_makeaddr 主机位和网络位合并成一个ip
int net = 0x00001276;
int host= 0x0000ABEF;
struct in_addr in_addr_tmp_t = inet_makeaddr(net,host);
printf("%x\n",in_addr_tmp_t.s_addr);
//6. inet_lnaof取主机位 inet_netof取网络位
printf("%x\n",inet_lnaof(in_addr_tmp_t));
printf("%x\n",inet_netof(in_addr_tmp_t));
return 0;
}
点分十进制转换
最新推荐文章于 2024-01-19 01:45:35 发布