導航:首頁 > 編程語言 > 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語言源代碼網卡相關的資料

熱點內容
linux裝apache 瀏覽:272
手機谷歌打不開網站怎麼辦 瀏覽:110
燈控台編程好了怎麼使用 瀏覽:342
如何用金山毒霸切斷網路連接 瀏覽:873
怎麼設置文件打開密碼怎麼設置 瀏覽:5
網路小說用什麼寫 瀏覽:956
擴展名為rm的文件如何打開 瀏覽:707
導入android項目沒有rjava 瀏覽:899
帶英文和數字的文件名 瀏覽:843
黃山數控編程培訓在哪裡 瀏覽:111
win10俠盜5無限讀取 瀏覽:557
js工廠模式 瀏覽:927
iphone6s清理緩存 瀏覽:465
ug編程怎麼選不上字體 瀏覽:435
pc文件加密哪個好 瀏覽:393
蘋果軟體更新4位密碼 瀏覽:96
手機如何將文件弄成文件夾 瀏覽:275
swordigo第四關 瀏覽:996
t420win10網卡驅動 瀏覽:805
ps怎麼打開dxf文件怎麼打開 瀏覽:698

友情鏈接