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

熱點內容
電極自動編程哪個軟體好用 瀏覽:550
cad看圖大師下載的文件在哪 瀏覽:227
硬碟損壞內部數據怎麼修 瀏覽:880
微信你真列害圖片大全 瀏覽:49
jsin類型可以傳數值嗎 瀏覽:532
win10文件從左邊滑出 瀏覽:53
win10關機後桌面文件丟失 瀏覽:266
watch卸載自帶應用程序 瀏覽:292
有什麼plc自學網站 瀏覽:219
176純金幣假人版本 瀏覽:334
紅米note4微信計步代碼 瀏覽:259
站長之家有app嗎 瀏覽:912
office應用視頻教程 瀏覽:690
html資料庫有哪些 瀏覽:489
鋼筆工具線不會變細 瀏覽:762
access2010資料庫上機 瀏覽:686
flstudio錄音教程 瀏覽:457
圖形編程軟體哪個好用 瀏覽:393
要怎麼重新下載而且不保存數據 瀏覽:188
手機軟體下載後文件在哪 瀏覽:515

友情鏈接