雙網絡卡或多網絡卡情況下獲得所有的IP位址

2021-04-02 09:04:33 字數 3695 閱讀 5740

在編寫基於sock的網路程式時,有時需列舉系統中繫結的所有ip位址,在一般控制項無法解決些問題的情況下,可使用如下方法一試:

#include "stdafx.h"

#include

#include

#include

#include

void main()

gethostname(myname, 80);    //取得主機名

thishost = gethostbyname(myname);    //取得主機的資訊(ip等)

memset((void *)&in, sizeof(in), 0);

//取得迴圈次數(ip位址數)

n=strlen(*thishost->h_addr_list);

n=n/2;

for(i=0;ih_addr_list[i]);

ptr = inet_ntoa(in);

printf("ip位址%d:%s",i+1,ptr);

}wsacleanup();    //解除安裝winsock動態庫(減少引用計數)

}二、使用「ip助手」

以下為引用自帖子:「vb 用winsock 如何獲取多網絡卡的ip 」http://community.csdn.net/expert/topic/4167/4167590.xml?temp=.3412592

介面上直接乙個text1,記得設定可以換行

option explicit

' 偵測目前裝置上所使用的 ip 位址

' 設定在您的計算機上,最多可能使用 5 組 ip 位址,並且用來產生緩衝區

private const max_ip = 10

private type ipinfo

dwaddr as long              ' ip 位址

dwnicindex as long          ' nic 介面索引

dwsubnetmask as long        ' 子網掩碼

dwbroadcastaddr as long     ' 封包廣播位址

dwreassemblysize  as long   ' 彙編大小

unused1 as integer          ' 暫不使用

unused2 as integer          ' 暫不使用

end type

private type mib_ipaddrtable

dwentrys as long            ' 窗體中登入的數量

aripinfo(max_ip) as ipinfo  ' ip 位址登入陣列

end type

private type ip_array

mbuffer as mib_ipaddrtable  ' ip 位址清單陣列

bufferlen as long           ' 緩衝區長度

end type

private declare sub copymemory lib "kernel32" alias "rtlmovememory" (destination as any, source as any, byval length as long)

private declare function getipaddrtable lib "iphlpapi" (pipadrtable as byte, pdwsize as long, byval sort as long) as long

' 將長整數轉換為字串

public function convertaddr2str(longaddress as long) as string

dim addrbyte(3) as byte

dim cnt as long

copymemory addrbyte(0), longaddress, 4

for cnt = 0 to 3

convertaddr2str = convertaddr2str + cstr(addrbyte(cnt)) + "."

next cnt

convertaddr2str = left$(convertaddr2str, len(convertaddr2str) - 1)

end function

private sub form_load()

text1.text = ""

me.caption = "取得計算機上所使用的 ip 位址"

text1.font.size = 11

start

end sub

private sub form_resize()

text1.height = me.height - 38 * screen.twipsperpixely

text1.width = me.width - 20 * screen.twipsperpixelx

end sub

private sub start()

dim lret as long, i as long

dim buffer() as byte

dim listdatas as mib_ipaddrtable

text1 = ""

on error goto errors

getipaddrtable byval 0&, lret, true

if lret <= 0 then exit sub

redim buffer(0 to lret - 1) as byte

debug.print buffer(0)

' 利用已經安裝 ip 位址的前四個位元組,來取得登入的資訊

copymemory listdatas.dwentrys, buffer(0), 4

text1 = "在您的計算機上,共有 " & listdatas.dwentrys & " 組已經設定使用的 ip 位址" & vbcrlf

text1 = text1 & string(45, "=") & vbcrlf

for i = 0 to listdatas.dwentrys - 1

' 將存在記憶體之中的位址結構,複製到清單之中

copymemory listdatas.aripinfo(i), buffer(4 + (i * len(listdatas.aripinfo(0)))), len(listdatas.aripinfo(i))

text1 = text1 & "ip 位址   :" & convertaddr2str(listdatas.aripinfo(i).dwaddr) & vbcrlf

text1 = text1 & "ip 子網掩碼:" & convertaddr2str(listdatas.aripinfo(i).dwsubnetmask) & vbcrlf

text1 = text1 & "ip 廣播位址 :" & convertaddr2str(listdatas.aripinfo(i).dwbroadcastaddr) & vbcrlf

text1 = text1 & string(45, "*") & vbcrlf & vbcrlf

next

exit sub

errors:

end sub

注:有關ip助手的使用方法請參見劉巍的文章:「ip helper api 使用方法詳解」http://www.zdnet.com.cn/developer/code/story/0,2000081534,39046680,00.htm

多網絡卡情況下,獲得每個網絡卡資訊

pip adapter info結構體指標儲存本機網絡卡資訊 ip adapter info pipadapterinfo null 得到結構體大小,用於getadaptersinfo引數 unsigned long stsize 0 呼叫getadaptersinfo函式,填充pipadapter...

多網絡卡存在情況下獲取指定網絡卡的MAC位址

應用軟體中經常有需要顯示mac位址的功能,方便使用者判斷當前聯網狀態,如果是在多網絡卡存在 無線,有線等 下,網路會經常切換,獲取到的mac位址可能與預期不同,現給出指定網絡卡獲取mac位址的方法。void get mac mac address mac address wlan0代表網絡卡的名字 ...

Linux多網絡卡情況下虛擬IP快速生效的方法

物理環境 雙機 多網絡卡 其中乙個網絡卡繫結2個ip 帶arp快取的交換機 問題 主備切換後,虛擬ip繫結成功,但外面不能ping同虛擬ip,也就是虛擬ip不能快速生效 注意 如果使用普通的二層交換機,則不存在該問題,因為普通交換機沒有arp快取。當然,通過調整交換機上的arp快取失效時間,可以緩解...