导航:首页 > 编程语言 > c语言源代码网卡

c语言源代码网卡

发布时间:2023-08-23 23:58:38

⑴ 怎么用C语言获取Linux系统的网卡IP地址

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<net/if.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<sys/ioctl.h>

//获取地址
//返回IP地址字符串
intgetlocalip(char*outip)
{
inti=0;
intsockfd;
structifconfifconf;
charbuf=(char)malloc(512);
structifreq*ifreq;
char*ip;

//初始化ifconf
ifconf.ifc_len=512;
ifconf.ifc_buf=buf;

if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
{
return-1;
}
ioctl(sockfd,SIOCGIFCONF,&ifconf);//获取所有接口信息
close(sockfd);
//接下来一个一个的获取IP地址
ifreq=(structifreq*)buf;
i=ifconf.ifc_len/sizeof(structifreq);
char*pos=outip;
intcount;
for(count=0;(count<5&&i>0);i--)
{
ip=inet_ntoa(((structsockaddr_in*)&(ifreq->ifr_addr))->sin_addr);

if(strncmp(ip,"127.0.0.1",3)==0)//排除127.x.x.x,继续下一个
{
ifreq++;
continue;
}else
{
printf("%s ",ip);
strcpy(pos,ip);
intlen=strlen(ip);
pos=' ';
pos+=len+1;
count++;
ifreq++;
}
}
free(buf);
return0;
}
//——————————-函数的调用方式————————————-
intmain(intargc,char**argv)
{
charip={'*'};

if(getlocalip(ip)==0)
{
printf("本机IP地址是:%s ",ip);
}
else
{
printf("无法获取本机IP地址");
}
return0;
}

阅读全文

与c语言源代码网卡相关的资料

热点内容
冒险岛老物品代码 浏览:834
南航app如何绑定护照 浏览:140
公众号小程序怎么制作 浏览:24
同城买菜什么网站 浏览:211
鸿蒙系统如何显示大文件 浏览:484
文件解压后可以删掉哪些 浏览:357
怎么清除c盘垃圾文件 浏览:720
js都要掌握哪些内容 浏览:906
四角号码字典有哪几个版本 浏览:869
数据库入侵dede 浏览:700
日本用什么app听歌 浏览:562
cmd新建空文件 浏览:481
广联达app在哪里找 浏览:125
javaudp网络编程 浏览:263
如何快速统计文件大小 浏览:395
dnf90版本奶爸寂静9加点 浏览:506
5s升级ios811好吗 浏览:773
苹果6s手机忘记解锁密码怎么办 浏览:223
哪里的数控编程培训学校好 浏览:495
小程序怎么登陆 浏览:957

友情链接