UDP實現簡單的ifconfig查詢功能

2021-09-19 10:12:51 字數 1951 閱讀 1436

一 **

#include /* for ifconf */  

#include /* for net status mask */

#include /* for sockaddr_in */

#include #include #include #include #include //for close

#include #include #define max_inte***ce (16)

void port_status(unsigned int flags);

int get_if_info(int fd)

/* 網口總數 ifc.ifc_len 應該是乙個出入引數 */

if_num = ifc.ifc_len / sizeof(struct ifreq);

printf("inte***ce num is inte***ce = %d\n", if_num);

while (if_num-- > 0)

}

void port_status(unsigned int flags)

if (flags & iff_broadcast)

if (flags & iff_loopback)

if (flags & iff_pointopoint)

if (flags & iff_running)

if (flags & iff_promisc)

}

int main()

return 0;

}

二 執行

[root@localhost test]# g++ test.cpp -o test

[root@localhost test]# ./test

inte***ce num is inte***ce = 2

net device: enp0s3

is up

is broadcast

is running

ip address is:

192.168.0.120

08:00:27:0c:3b:d3

net device: lo

is up

is loop back

is running

ip address is:

127.0.0.1

00:00:00:00:00:00

三 說明介面

siocgifconf

siocsifaddr

siocgifaddr

siocsifbrdaddr

siocgifbrdaddr

siocsifnetmask

siocgifnetmask

獲取所有介面列表

設定介面位址

獲取介面位址

設定廣播位址

獲取廣播位址

設定子網掩碼

獲取子網掩碼

struct  ifconf

struct  ifreq

struct  ifreq

struct  ifreq

struct  ifreq

struct  ifreq

struct  ifreq

2  ifconf定義如下:

struct ifconfifc_ifcu

}#define ifc_buf ifc_ifcu.ifcu_buf

#define ifc_req ifc_ifcu.ifcu_req

3 本機所有的網絡卡資訊都列舉出來了,效果和ifconfig命令類似。

C 實現簡單的UDP

udp和tcp不同 udp可以比喻為現實中的寫信 當然不是email tcp可以比喻為顯示中的打 tcp可以保持連線,因此可以通過連線來發發訊息 udp不能保持連線,因此沒有傳送訊息和接收訊息都要加上對方位址 tcp的服務端和客戶端很不同,服務端用tcplistner,客戶端用tcpclient。u...

UDP通訊的簡單實現(程式)

資訊處理介面,定義了資訊的關閉,傳送,和接收 public inte ce messageaction udp類實現了messageaction封裝了下面的資訊,並提供了相應的get和set方法private int sendport 要傳送資料的埠號埠號 private int localport...

Python 實現udp簡單通訊

學習 指路 server端 匯入socket import socket 設定埠號,主機號 port 12345 host 將上面倆放在位址裡 addr host,port 為該物件繫結固定位址 ip,埠號 server socket.bind addr while true recvfrom 表示...