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

熱點內容
哪個app可以賣二手課程 瀏覽:474
互聯網app如何算毛利 瀏覽:300
excel文件處理插件 瀏覽:666
在電腦上怎麼找不到微信文件夾 瀏覽:116
u盤打開後有文件沒內容 瀏覽:210
編程怎麼輸出逗號 瀏覽:421
微信玩紅包怎麼定大小 瀏覽:825
電腦病毒測試代碼 瀏覽:118
得物app為什麼總是更新 瀏覽:965
銀企對賬程序 瀏覽:164
r讀取excel文件 瀏覽:363
上古卷軸5控制台附魔代碼 瀏覽:514
緩存文件合並找不到文件 瀏覽:871
桌面保存一下文件找不到 瀏覽:645
程序美工標准 瀏覽:191
漂流瓶的文件在哪裡 瀏覽:319
數據的正負偏差怎麼計算 瀏覽:242
文件名用不用帶TXT 瀏覽:968
小米十數據線是哪個 瀏覽:463
caddws文件 瀏覽:962

友情鏈接