博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux c 网络编程:用域名获取IP地址或者用IP获取域名 网络地址转换成整型 主机字符顺序与网络字节顺序的转换
阅读量:4030 次
发布时间:2019-05-24

本文共 1920 字,大约阅读时间需要 6 分钟。

用域名获取IP地址或者用IP获取域名
 
#include
#include
#include
int main(int argc,char **aggv){ struct hostent *host; char hostname[]="www.163.com"; char hostname2[]="www.baidu.com"; struct in_addr in; struct sockaddr_in addr_in; int h_errno; char addr[]="202.108.249.216"; if((host=gethostbyname(hostname))!=NULL) { memcpy(&addr_in.sin_addr.s_addr,host->h_addr,4); in.s_addr=addr_in.sin_addr.s_addr; printf("Domain name : %s \n",hostname); printf("IP length is: %d \n",host->h_length); printf("Type : %d \n",host->h_addrtype); printf("IP : %s \n",inet_ntoa(in)); } if(((host=gethostbyaddr(addr,sizeof(addr),AF_INET)))!=(struct hostnet *)NULL) { memcpy(&addr_in.sin_addr.s_addr,host->h_addr,4); in.s_addr=addr_in.sin_addr.s_addr; printf("\n---------------------\n"); printf("Domain name : %s \n",hostname); printf("IP length is: %d \n",host->h_length); printf("Type : %d \n",host->h_addrtype); printf("IP : %s \n",inet_ntoa(in)); } return 0;}

#include
#include
#include
int main(){ int i ; for(i=0;i<6;i++) printf("%d %s \n",i,hstrerror(i));//捕获错误编号 char cp[]="192.168.0.84"; printf("%ld\n",inet_addr(cp)); //将网络地址转换成整型 struct in_addr ip; ip.s_addr=16885952; printf("%s\n",inet_ntoa(ip)); //将整型转换成网络地址 long local; int port; local=123456; port=1024; printf("net: %d\n",htonl(local));//主机字符顺序与网络字节顺序的转换 printf("net: %d\n",htonl(port)); printf("local: %d\n",ntohl(htonl(local))); printf("local: %d\n",ntohl(htonl(port))); return 0;}

转载地址:http://bqobi.baihongyu.com/

你可能感兴趣的文章
rfkill: WLAN hard blocked
查看>>
gstreamer相关工具集合
查看>>
arm 自动升级脚本
查看>>
RS232 四入四出模块控制代码
查看>>
gstreamer插件之 videotestsrc
查看>>
autoupdate script
查看>>
linux 驱动开发 头文件
查看>>
/etc/resolv.conf
查看>>
container_of()传入结构体中的成员,返回该结构体的首地址
查看>>
linux sfdisk partition
查看>>
ipconfig,ifconfig,iwconfig
查看>>
opensuse12.2 PL2303 minicom
查看>>
电平触发方式和边沿触发的区别
查看>>
网络视频服务器移植
查看>>
Encoding Schemes
查看>>
移植QT
查看>>
如此调用
查看>>
计算机的发展史
查看>>
带WiringPi库的交叉编译如何处理一
查看>>
带WiringPi库的交叉笔译如何处理二之软链接概念
查看>>