Statistics.cpp

This example demonstrates how to use PacketX to collect statistics from C++.
See also:
PacketX , Statistics , Adapter
00001 
00002 // File:         Statistics.cpp
00003 //
00004 // Description:  This sample demonstrates how to collect statistics
00005 //
00006 // Notes:        Compile with Microsoft Visual C++ 6.0 or .NET
00007 //
00008 // Created:      May 10, 2004
00009 //
00010 // Copyright (c) 2000-2004 BeeSync Technologies.
00012 
00013 #define WIN32_LEAN_AND_MEAN
00014 
00015 #ifndef _WIN32_WINNT
00016 #define _WIN32_WINNT 0x0400
00017 #endif
00018 
00019 #include <atlbase.h>
00020 #include <stdio.h>
00021 #include <time.h>
00022 
00023 #import "PacketX.dll" no_namespace, named_guids
00024 
00025 static HRESULT __Stats_Handler__(IPktXStatistics* pStats,DWORD dwParam);
00026 
00027 int main(int argc, char* argv[])
00028 {
00029    // Initialize COM
00030 #define MT_APARTMENT
00031 #ifdef MT_APARTMENT
00032    _S(CoInitializeEx(NULL,COINIT_MULTITHREADED));
00033 #else
00034    _S(CoInitialize(NULL));
00035 #endif
00036 
00037    // Create PacketX
00038    IPktXPacketXPtr pPktX(__uuidof(PacketX));
00039 
00040    // Display network adapters
00041    LONG lCount= IPktXAdapterCollectionPtr(pPktX->GetAdapters())->GetCount();
00042    for (int i=0; i < lCount; i++)
00043    {
00044       // If adapter is good print adapter properties
00045       IPktXAdapterPtr pAdapter = IPktXAdapterCollectionPtr(pPktX->GetAdapters())->GetItem(i+1);
00046       if (pAdapter->GetIsGood())
00047       {
00048          _bstr_t bstrtDesciption = pAdapter->GetDescription();
00049          printf("(%d) %s\n",i+1,(char*)bstrtDesciption);
00050       }
00051    }
00052    // Select network adapter
00053    pPktX->PutAdapter(NULL);
00054    while (pPktX->GetAdapter() == NULL)
00055    {
00056       char buffer[256];
00057       printf("Choose adapter#");
00058       try { pPktX->PutAdapter(IPktXAdapterCollectionPtr(
00059          pPktX->GetAdapters())->GetItem(atoi(gets(buffer)))); }
00060       catch ( _com_error& ) {}
00061    }
00062    // Set statistics mode
00063    IPktXAdapterPtr(pPktX->GetAdapter())->PutMode(PktXModeStatistics);
00064    IPktXAdapterPtr(pPktX->GetAdapter())->PutReadTimeout(2000);
00065 
00066    // Create sink
00067    IPktXPacketSinkPtr pSink(__uuidof(PacketSink));
00068    pSink.CreateInstance(__uuidof(PacketSink));
00069    pSink->CallBackOnStatistics((DWORD)__Stats_Handler__,(DWORD)0);
00070 
00071    // Bind sink to PacketX
00072    pSink->Advise(pPktX);
00073 
00074    // Start capture
00075    pPktX->Start();
00076 
00077 #ifdef MT_APARTMENT
00078       Sleep(60000);
00079 #else
00080    // Message loop
00081    MSG msg;
00082    while (GetMessage(&msg, 0, 0, 0) && lEventCounter <= lNumOfEvents)
00083       DispatchMessage(&msg);
00084 #endif
00085 
00086    // Stop capture
00087    pPktX->Stop();
00088 
00089    // Unadvise sink from PacketX
00090    pSink->Unadvise(pPktX);
00091 
00092    // Cleanup
00093    pSink = NULL;
00094    pPktX = NULL;
00095 
00096    // Uninitialize COM
00097    CoUninitialize();
00098    return 0;
00099 }
00100 
00101 // Statistics event handler
00102 static HRESULT __Stats_Handler__(IPktXStatistics* pStats,DWORD dwParam)
00103 {
00104    // Get time in seconds
00105    time_t ltime = IPktXStatisticsPtr(pStats)->GetTimeSec();
00106 
00107    // Display statistics
00108    printf("Pkts %d Bytes %d %s",
00109       IPktXStatisticsPtr(pStats)->GetPackets(),
00110       IPktXStatisticsPtr(pStats)->GetBytes(), ctime(&ltime));
00111    return 0;
00112 }

Copyright © 2000-2007 beeSync. All rights reserved.