視窗:網路-WinPcap-“Hello World!”

來源:互聯網
上載者:User

以前看人家用WinPcap怪方便的,今天有點時間也上網下了一個,打開VC咱們也來個~~

咱們不寫“Hello World”了,WinPcap寫不出來,咱們就寫個列印本機可用網卡列表的程式吧,這是最簡單的了。

先去http://www.winpcap.org/install/bin/WinPcap_4_0.exe下載安裝WinPcap,

再到 http://www.winpcap.org/install/bin/WpdPack_4_0.zip下載到WinPcap的開發包,解壓後進入那個Examples-pcap檔案夾,裏面都是例子,隨便選一個,比如DUPdump吧,把它在複製一份“DUPdump 副本”,改個名兒,這是咱們的第一個程式,就叫test把。進去把裡面的什麽“UDPdump.dsp”、“UDPdump.dsw”改成“test.dsp”、“test.dsw”,再用記事本把這兩個檔案打開,將“UDPdump”字樣全部替換成我們的“test”,好這樣人家的UDPdump工程就變成了我們的test工程,用VC打開test工程,在右邊我們可以看到有源檔案udpdump.c、頭檔案pcap.h和一個庫檔案,把那個源檔案幹掉,添加一個咱們的main.cpp,裏面填入下列代碼:

main.cpp:

#include "pcap.h"

void main(void)
...{
    pcap_if_t *alldevs;
    pcap_if_t *d;
    
    int i=0;
    char errbuf[PCAP_ERRBUF_SIZE];
    //取得列表
    if (pcap_findalldevs(&alldevs, errbuf) == -1)...{
        fprintf(stderr,"Error in pcap_findalldevs:%s ", errbuf);
        exit(1);
    }
    
    //輸出列表
    for(d=alldevs;d;d=d->next)...{
        printf("%d. %s", ++i, d->name);
        if (d->description)...{
            printf(" (%s) ", d->description);
        }else...{
            //Y- 沒有有效描述
            printf(" (無描述) ");
        }
    }
    
    if(i==0)...{
        // Y- 沒有有效介面,亦可能是因爲沒有安裝winpcap
        printf(" 介面沒有找到! 請確認是否正確安裝WinPcap。 ");
        return;
    }
    
    //釋放列表
    pcap_freealldevs(alldevs);
}

 

構建,ok,運行,本機的網卡介面列表出來了~

 

//註:如果你是XP sp2的工作站,構建時提示找不到什麽Iphlpapi.lib檔案,那就得安裝微軟的一個包,大包,上

http://www.microsoft.com/msdownload/platformsdk/sdkupdate/XPSP2FULLInstall.htm

去下吧~~~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.