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

熱點內容
cocoapods版本更新 瀏覽:361
國外it大神教你學習如何編程 瀏覽:312
手機存儲應用程序 瀏覽:284
頁面自適應屏幕如何調整代碼數據 瀏覽:681
jsjson工具 瀏覽:299
資料庫中如何備份一張表的數據 瀏覽:739
網路設備能用到什麼 瀏覽:64
暴風轉碼如何添加文件夾 瀏覽:515
延安整合網路營銷有哪些 瀏覽:74
查找word打開過的文件在哪裡 瀏覽:137
b樹java代碼 瀏覽:683
電腦文件存儲 瀏覽:657
蘭州中考徵集志願在哪個網站 瀏覽:215
cs文件上傳下載 瀏覽:244
拷貝文件到根目錄下重命名linux 瀏覽:603
api函數的頭文件 瀏覽:249
華為怎麼綁定迷你編程 瀏覽:215
機構怎麼申請少兒編程考級 瀏覽:495
崑山數控編程哪裡好學 瀏覽:459
jspcfor跳出 瀏覽:65

友情鏈接