啟動monitor線程調用packetcount()函數來監視一片網卡,但如果我在monitor線程中
再調用另外一個packetcount1()函數來監視另一片網卡卻不行,不知為什麼?
這個函數pcap_open_live((char*)d->name, 68, 1, 1000, ebuf) )不知里面的第二,三個參數有什麼意義?是不是監視第二片要用2呢?pcap_open_live((char*)d->name, 68, 2, 1000, ebuf) ),不過好象也不行,非得在兩個線程中啟動兩個不同的packetcount()函數才能監視得到每片網卡的獨立的數據.
大俠們有沒有更好的方法來做?UINT CNetmonitorDlg::monitor(LPVOID pParam)
{
CNetmonitorDlg * pDlg=(CNetmonitorDlg *)pParam;

char *ebuf;
char *myAdapter;

CString Adapter;

ebuf=(char*)malloc(PCAP_ERRBUF_SIZE);

//**********************************************
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
// char errbuf[PCAP_ERRBUF_SIZE];
    
    /* Retrieve the device list on the local machine */
    //if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
// int pcap_findalldevs(pcap_if_t **, char *);
//**********************************************
if (Adapter=="")
{
myAdapter=(char*)pcap_lookupdev(ebuf);
DWORD dwVersion=GetVersion(); //get the OS version
DWORD dwWindowsMajorVersion =  (DWORD)(LOBYTE(LOWORD(dwVersion)));
if (dwVersion >= 0x80000000 && dwWindowsMajorVersion >= 4)// Windows '95
Adapter=myAdapter;
else //winNT
Adapter=(WCHAR *)myAdapter;

if (pcap_findalldevs(&alldevs, ebuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n", ebuf);
exit(1);
}

/* Print the list */
for(d=alldevs; d; d=d->next)
{
printf("%d. %s", ++i, d->name);
if (d->description)
printf(" (%s)\n", d->description);
else
AfxMessageBox(" (No description available)\n");
}

if(i==0)
{
AfxMessageBox("\nNo interfaces found! Make sure WinPcap is installed.\n");
return -1;
}

inum=3; //腢隅猁屠?腔鋒縐.
if(inum < 1 || inum > i)
{
AfxMessageBox("\nInterface number out of range.\n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}

/* Jump to the selected adapter */
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);

/* Open the device */

pcap_t *fp=NULL;
if ( (fp= pcap_open_live((char*)d->name, 68, 1, 1000, ebuf) ) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
if(pcap_loop(fp,-1,pDlg->parcket_handle,(u_char *)pDlg)<0)
{
TRACE1("pcap_loop error: %d\n", pcap_geterr(fp));
return FALSE;
}
////////////////////////////
inum=2; //腢隅猁屠?腔鋒縐2.
if(inum < 1 || inum > i)
{
AfxMessageBox("\nInterface number out of range.\n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}

/* Jump to the selected adapter */
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);

/* Open the device */

fp=NULL;
if ( (fp= pcap_open_live((char*)d->name, 68, 1, 1000, ebuf) ) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
if(pcap_loop(fp,-1,pDlg->parcket_count,(u_char *)pDlg)<0)
{
TRACE1("pcap_loop error: %d\n", pcap_geterr(fp));
return FALSE;
} pcap_close(fp);

}

return 0;
}void CNetmonitorDlg::parcket_count(u_char * user,const struct pcap_pkthdr *h,const u_char * p)
{
CNetmonitorDlg * pDlg=(CNetmonitorDlg *)user;
//悵湔杅擂
byte * data=new byte[h->len];
memcpy(data,p,h->len);
int a=h->len;
char mybuf[1618],tmp[2];
memset(mybuf,0,1618);
for(int my=0;my<a;my++)
{
memset(tmp,0,2);
sprintf(tmp,"%02X",data[my]);
strcat(mybuf,tmp);
}
// pDlg->m_monitor.SetWindowText(mybuf);

char mycount[256]="\0";
sprintf(mycount,"%d",pDlg->m_count2);
pDlg->m_strcount2.SetWindowText(mycount);

char buffer[256];
//醴梓MAC華硊
memset(buffer,0,256);
sprintf(buffer,"%02X-%02X-%02X-%02X-%02X-%02X",p[0],p[1],p[2],p[3],p[4],p[5]); //if(pDlg->m_count<100)
//0x00,0x90,0xcc,0x41,0x76,0x29  0x00,0x90,0xcc,0x41,0x70,0x99
if(strcmp(buffer,"00-90-CC-41-70-99")==0)
{
strcat(moitordata,mybuf);
strcat(moitordata,"\r\n");
if(a==60)
pDlg->m_count++;
}
if(pDlg->m_count==10)
{
pDlg->m_monitor.SetWindowText(moitordata);
memset(moitordata,0,sizeof(moitordata));
// pDlg->OnStopMonitor();
pDlg->m_count2=0;
}
delete data;
}