linux c函式獲取系統IP位址

2021-09-05 01:20:23 字數 2134 閱讀 4524

一,通過分析/etc/hosts檔案裡對映關係獲取ip位址。

#include

#include

int main()

;gethostname(hostname,sizeof(hostname));

he = gethostbyname(hostname);

printf("hostname=%s\n",hostname);

printf("%s\n",inet_ntoa(*(struct in_addr*)(he->h_addr)));}類別

request

說明資料型別

介面siocatmark 

siocspgrp 

siocgpgrp

是否位於帶外標記 

設定套介面的程序id 或程序組id 

獲取套介面的程序id 或程序組id

int 

int 

int

fionbin 

fioasync 

fionread 

fiosetown 

fiogetown

設定/ 清除非阻塞i/o 標誌 

設定/ 清除訊號驅動非同步i/o 標誌 

獲取接收快取區中的位元組數 

設定檔案的程序id 或程序組id 

獲取檔案的程序id 或程序組id

int 

int 

int 

int 

int

siocgifconf 

siocsifaddr 

siocgifaddr 

siocsifflags 

siocgifflags 

siocsifdstaddr 

siocgifdstaddr 

siocgifbrdaddr 

siocsifbrdaddr 

siocgifnetmask 

siocsifnetmask 

siocgifmetric 

siocsifmetric 

siocgifmtu 

sioc***

獲取所有介面的清單 

設定介面位址 

獲取介面位址 

設定介面標誌 

獲取介面標誌 

設定點到點位址 

獲取點到點位址 

獲取廣播位址 

設定廣播位址 

獲取子網掩碼 

設定子網掩碼 

獲取介面的測度 

設定介面的測度 

獲取介面mtu 

(還有很多取決於系統的實現)

struct ifconf 

struct ifreq 

struct ifreq 

struct ifreq 

struct ifreq 

struct ifreq 

struct ifreq 

struct ifreq 

struct ifreq 

struct ifreq 

struct ifreq 

struct ifreq 

struct ifreq 

struct ifreq

arpsiocsarp 

siocgarp 

siocdarp

建立/ 修改arp 表項 

獲取arp 表項 

刪除arp 表項

struct arpreq 

struct arpreq 

struct arpreq

siocaddrt 

siocdelrt

增加路徑 

刪除路徑

struct rtentry 

struct rtentry

i_***

#include #include #include #include #include #include #include int main()

Linux C獲取時間函式

標頭檔案 include strftime 函式 函式原型 size t strftime char array,size t maxsize,const char format,const struct tm tmptr 引數 1.array 為字元型陣列名 2.maxsize 為array陣列的...

linux C 獲取與修改IP位址

ifndef include setip h define include setip h 設定ip位址 函式名稱 int setip char ip 函式功能 設定系統ip位址 參 數 char ip 設定的ip位址,以點分十進位制的字串方式表示,如 192.168.0.5 返 回 值 0 成功 ...

linux c語言獲取系統時間

gmtime 取得目前時間和日期 獲得相關函式 time,asctime,ctime,localtime 表頭檔案 include 定義函式 struct tm gmtime const time t timep 函式說明 gmtime 將引數timep 所指的time t 結構中的資訊 轉換成真實...