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

熱點內容
u盤文件無刪除選項 瀏覽:991
php文件utf8記事本打開出錯 瀏覽:197
公司相關人員任命文件有哪些 瀏覽:397
dnd異界升級順序 瀏覽:887
linuxbin文件解壓 瀏覽:579
bat文件名 瀏覽:715
大型文件互傳軟體哪個好 瀏覽:108
保存後的掃描文件在哪裡 瀏覽:688
html轉化為jsp 瀏覽:722
鬧鈴記事本app哪個好用 瀏覽:190
雅馬哈編程軟體怎麼安裝 瀏覽:113
蘋果筆記本電腦怎麼顯示文件 瀏覽:486
word的文件選項在哪 瀏覽:139
adb文件在哪裡 瀏覽:880
word替換行首 瀏覽:342
java教程百度雲最新版 瀏覽:386
編程功能圖怎麼畫 瀏覽:322
球刀倒45度角怎麼編程 瀏覽:204
安心付app不能綁定卡怎麼回事 瀏覽:429
松江主機煙感點怎麼編程 瀏覽:589

友情鏈接