当前位置:首页 » 编程语言 » c语言gethostbyname

c语言gethostbyname

发布时间: 2025-10-06 04:33:00

Ⅰ 如何用c语言编程得到本地主机得IP和主机名

  1. c语言本身是不提供的。

  2. 在windows下使用系统命令或windows.h

  3. //列子

  4. #include"stdio.h"
    #include"conio.h"
    main()
    {
    inti,j;
    charip[20];
    chartemp[100];
    charch='';
    FILE*fp;
    system("ipconfig>d:\myip.txt");
    if((fp=fopen("d:\myip.txt","r"))==NULL)
    {
    printf("thefilecannotopen: Pressanykeytoexit:");
    getch();
    exit(1);
    }
    for(i=0;i<7;i++)
    {fgets(temp,80,fp);/*跳过一些行*/
    /*printf("%s ",temp);*/}
    fgets(temp,80,fp);
    i=0;j=0;
    while(temp[i++]!=':')
    ;
    while(temp[i]!=' ')
    ip[j++]=temp[i++];
    ip[j]=0;
    printf("IP=%s ",ip);
    fclose(fp);
    system("deld:\myip.txt");

    getch();
    }
  5. linux
  6. #include<stdio.h>;
    #include<sys/types.h>;
    #include<sys/socket.h>;
    #include<sys/ioctl.h>;
    #include<netinet/in.h>;
    #include<net/if.h>;
    #include<net/if_arp.h>;
    #include<arpa/inet.h>;
    #include<errno.h>;

    #defineETH_NAME"eth0"

    intmain()
    {
    intsock;
    structsockaddr_insin;
    structifreqifr;

    sock=socket(AF_INET,SOCK_DGRAM,0);
    if(sock==-1)
    {
    perror("socket");
    return-1;
    }

    strncpy(ifr.ifr_name,ETH_NAME,IFNAMSIZ);
    ifr.ifr_name[IFNAMSIZ-1]=0;

    if(ioctl(sock,SIOCGIFADDR,&ifr)<0)
    {
    perror("ioctl");
    return-1;
    }

    memcpy(&sin,&ifr.ifr_addr,sizeof(sin));
    fprintf(stdout,"eth0:%s ",inet_ntoa(sin.sin_addr));

    return0;
    }

Ⅱ 如何用C语言获得本机IP地址

structin_addraddr;
hostent*pHost=::gethostbyname("localhost");//在此写入你自己电脑主机名字
switch(pHost->h_addrtype){
caseAF_INET:
printf("internet网络地址类型(AF_INET) ");
break;
caseAF_INET6:
printf("internet网络地址类型(AF_INET) ");
break;
caseAF_NETBIOS:
printf("netbios网络地址类型(AF_NETBIOS) ");
break;
default:
printf("其它地址类型%d ",pHost->h_addrtype);
break;
}
printf(" 地址长度:%d(字节) ",pHost->h_length);
addr.s_addr=*(u_long*)pHost->h_addr_list[0];
printf(" 第一个IP地址为:%s ",inet_ntoa(addr));

Ⅲ Linux中怎么用C语言打开网页

给你一个哈,我自己调试好的,并且加了详细注释~~记得给分啊,我没分问问题了~

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <string.h>
#include <stdarg.h>
#include <netdb.h>
#include <setjmp.h>
#include <signal.h>

/*gethostbyname 超时返回
这里使用的办法是设置一个时钟,如果gethostbyname在指定的时间内尚未返回,
时钟会强制其返回,得到的返回值显然是空指针,等价于告诉用户主机未连如互联网或者该域名无法解析。*/
static sigjmp_buf jmpbuf;
static void alarm_func() //该函数执行之后会执行跳转
{
siglongjmp(jmpbuf, 1);
}

static struct hostent *gngethostbyname(char *HostName, int timeout)
{
struct hostent *lpHostEnt;

signal(SIGALRM, alarm_func); //接受alarm信号,然后调用函数
if(sigsetjmp(jmpbuf, 1) != 0)//跳转目的地
{
alarm(0);//timout
signal(SIGALRM, SIG_IGN);
return NULL;
}
alarm(timeout);//setting alarm
printf("\nwill gethost!\n");
lpHostEnt = gethostbyname(HostName);
signal(SIGALRM, SIG_IGN);

return lpHostEnt;
}

/*(linux socket编程实现connect超时的一种方法
创建套接字,将其设置成非阻塞状态。
调用connect连接对端主机,如果失败,判断当时的errno是否为EINPROGRESS,也就是说是不是连接正在进行中,如果是,转到步骤3,如果不是,返回错误。
用select在指定的超时时间内监听套接字的写就绪事件,如果select有监听到,证明连接成功,否则连接失败。*/

int main(int argc, char *argv[])
{
//最好检查一下参数,要求传入3个参数 URL PORT TIMEOUT(connect && send && recv 3个参数的超时)
int fd, retval,res,error;
struct sockaddr_in addr;
struct timeval timeo = {15, 0}; //time ou struct
struct hostent *site;
socklen_t len = sizeof(timeo);
fd_set set;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (argc == 4)
timeo.tv_sec = atoi(argv[3]);
site=gngethostbyname(argv[1],3); //解析域名的超时设置,测试域名超时,可以写一个可以ping的通但是没有办法解析域名
//的IP地址到resolv.conf里面,然后加上一个默认路由,直接PING一个网络,就能发现如果不加超时机制就会一直卡在那里
if(NULL == site)
{
printf("\ncan not find the site!\n");
return -2;
}
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK); //设置为非阻塞模式
addr.sin_family = AF_INET;
//addr.sin_addr.s_addr = inet_addr(argv[1]);
memcpy(&addr.sin_addr, site->h_addr_list[0], site->h_length);
addr.sin_port = htons(atoi(argv[2]));
printf("%d\n", time(NULL));
/*if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == 0) {
printf("connected1\n");
// return 0;
}*/
//res=connect(fd, (struct sockaddr*)&addr, sizeof(addr));
//printf("\nconnect result:[%d]\n",res);
if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) != 0)
{
//调用一次系统函数失败后直接看errno,确定是什么问题,下面的代码可以实现在没有默认路由的情况下直接返回失败.
if (errno != EINPROGRESS) {
printf("connect:normal network unreach!!");
return -1;
}
printf("\nwill select\n");
FD_ZERO(&set);/*将set清零使集合中不含任何fd*/
FD_SET(fd,&set); /*将一个给定的文件描述符加入集合之中*/
retval = select(fd + 1, NULL, &set, NULL, &timeo);
if (retval == -1) {
printf("select");
return -1;
} else if(retval == 0) {
printf("timeout\n"); //这样的select等于是变成了再timeout时间内是阻塞模式,超过timeout就直接返回
printf("%d\n", time(NULL));
return 0;
}
else
{
printf("connected--->:[%d]\n",retval);
getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, (socklen_t *)&len); //判断在connected成功之后,获取套接口目前的一些信息来判断是否真的是连接上了,返回0表示真的连上了
printf("error--->:[%d]\n",error);

if(0!=error)
return -1;
}
}

int ul = 0;
ioctl(fd, FIONBIO, &ul); //设置为阻塞模式
//return 0;
setsockopt(fd,SOL_SOCKET,SO_SNDTIMEO,(char *)&timeo,sizeof(timeo));
setsockopt(fd,SOL_SOCKET,SO_RCVTIMEO,(char *)&timeo,sizeof(timeo));
printf("\nbefore\n");
sleep(5); //在sleep 5的时候,拔掉网线就可以测试出recv超时的功能,如果不加recv 的超时功能,拔掉网线后就会一直卡在那里,当然你在实际应用的时候没必要加这个
printf("\nafter\n");
char *msg="GET / HTTP/1.0\r\n\r\n";
if( send(fd, msg, strlen(msg), 0)<0 )
{
printf("error in send msg\n");
exit(1);
}
int i=0;
char buf[1000];

while((recv(fd,buf,1000,MSG_WAITALL))>0)
{
printf("[%d]:[%s]",i,buf);
i++;
}
printf("\n------end---------\n");
close(fd);
return;
}

热点内容
数据库在中的应用 发布:2025-10-06 07:08:01 浏览:279
android查看网卡 发布:2025-10-06 07:00:49 浏览:83
贴吧签到脚本 发布:2025-10-06 07:00:41 浏览:926
访问一个模拟 发布:2025-10-06 06:42:31 浏览:756
psv存储卡有什么用 发布:2025-10-06 06:36:02 浏览:879
linuxnginx免编译 发布:2025-10-06 06:35:17 浏览:488
编译程序属于系统软件吗 发布:2025-10-06 06:22:14 浏览:89
腾讯云服务器建网站 发布:2025-10-06 05:47:51 浏览:467
iphone薯仔缓存 发布:2025-10-06 05:46:18 浏览:693
发牌c语言 发布:2025-10-06 05:37:14 浏览:473