http协议简介
请求 请求命令行+请求头+请求正文(可选)
响应 响应命令行+响应头+响应正文
struct hostent *gethostbyname(const char *name);
功能:通过主机名获取主机信息(ip 协议类型等);
参数:
name——指向主机名,例如:“www.baidu.com”;
返回值:出错返回NULL;成功返回struct hostent *;
struct hostent {
char h_name; / official name of host */
char *h_aliases; / alias list */
int h_addrtype; /* host address type */
int h_length; /* length of address */
char *h_addr_list; / list of addresses */
}
const char *inet_ntop(int af, const void *src,
char *dst, socklen_t size);
功能:把二进制IP转换为点分十进制IP
参数:
af—–AF_INET、AF_INET6
src—-二进制IP的地址,使用时h_addr_list[i]
dst—该指针指向的内存用于存储点分十进制IP(字符串)
size—一般为16(111.222.111.123\0)共16个字节
返回值:出错返回NULL
成功返回转换得到的字符串的地址,也就是dst
示例:
#include <netdb.h>
#include <arpa/inet.h>
#include <stdio.h>
int main(int argc, char const *argv[])
{
struct hostent *info;
if(argc != 2)
{
printf("usage:%s domain_name\n",argv[0]);
return -1;
}
info = gethostbyname(argv[1]);
if(info == NULL)
{
perror("gethostbyname failed");
return -1;
}
/*打印官方域名*/
printf("official name:%s\n",info->h_name);
int i;
char dst[16] = {0};
/*打印别名*/
for(i=0; (info->h_aliases)[i] != NULL; i++)
printf("h_aliases[%d]aliases:%s\n",i, info->h_aliases[i]);
/*打印IP地址*/
for(i=0; (info->h_addr_list)[i] !=NULL; i++)
{
if(inet_ntop(AF_INET,(info->h_addr_list)[i],dst,16) != NULL)
printf("h_addr_llist[%d]addr:%s\n",i, dst);
else
{
perror("inet_ntop failed");
return -1;
}
}
return 0;
}
输入域名,打印官方域名,别名和网址!