学习到了网络编程,在用域名取得主机地址的程序中,在使用inet_ntoa()时报错:段错误(核心已转存储) 代码如下:
#include<sys/socket.h>
#include<netdb.h>
#include <arpa/inet.h>
int main()
{
char hostname[]="www.baidu.com";
char hostname2[]="www.skgjkd.com";
struct hostent *host;
struct in_addr in;
struct sockaddr_in addr_in;
extern int h_errno;
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: %d\n",host->h_length);
printf("Type: %d\n",host->h_addrtype);
printf("IP: %s\n",inet_ntoa(in)); //将IP地址转换后输出(报错点)
}
else
{
printf("Domain name: %s\n",hostname);
printf("error: %d\n",h_errno);
printf("%s\n",hs