求教求教!!

    // NodeList.h: interface for the CNodeList class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_NODELIST_H__9781C411_82DF_47F7_A449_3054B18A550E__INCLUDED_)
#define AFX_NODELIST_H__9781C411_82DF_47F7_A449_3054B18A550E__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include<iostream>
#include<fstream>
#include<iomanip>
#include<winsock2.h>
#include "IPNode.h"

// 结点链表类CNodeList
class CNodeList 
{
private:
 CIPNode * pHead; // 链表头
 CIPNode * pTail; // 链表尾
 
public:
 // Default constructor
 CNodeList();
 // Default destructor
 virtual ~CNodeList();

 // 把新捕获的IP数据包加入链表
 void addNode(unsigned long, unsigned long, unsigned char);
 // 输出链表
 ostream& print(ostream& );
};

#endif // !defined(AFX_NODELIST_H__9781C411_82DF_47F7_A449_3054B18A550E__INCLUDED_)

// NodeList.cpp: implementation of the CNodeList class.

// Default constructor
CNodeList::CNodeList()
{
 pHead = pTail = NULL;
}

// Default destructor
CNodeList::~CNodeList()
{
 // 删除链表中所有结点
 if (pHead != NULL)
 {
  CIPNode * pTemp = pHead;
  pHead = pHead->pNext;
  delete pTemp;
 }
}

// 把新捕获的IP数据包加入链表
void CNodeList::addNode(unsigned long dwSourIP,unsigned long dwDestIP,unsigned char chPro)
{
 if (pHead == NULL) // 链表空
 {
  pTail = new CIPNode(dwSourIP,dwDestIP,chPro);
  pHead = pTail;
  pTail->pNext = NULL;
 }
 
 else // 链表不空时
 {
  CIPNode * pTemp;
  for(pTemp = pHead; pTemp; pTemp = pTemp->pNext)
  {
   // 如果链表中已存在该类型的IP包,则数据包个数加1
   if (pTemp->getSourIPAddr() == dwSourIP &&
    pTemp->getDestIPAddr() == dwDestIP &&
    pTemp->getProtocol() == chPro)
   {
    // 数据包个数加1
    pTemp->addCount();
    break;
   }
  }
  // 如果链表中不存在该类型的IP包,则创建新的结点加入链表
  if (pTemp == NULL)
  {
   pTail->pNext = new CIPNode(dwSourIP,dwDestIP,chPro);
   pTail = pTail->pNext;
   pTail->pNext = NULL;
  }
 }
}

// 输出链表
ostream& CNodeList::print(ostream& out)
{
 CIPNode * pTemp;
 if(pHead == NULL)
 {
  out << "没有捕获到IP数据包!" << endl;
 }
 else
 {
  out << "源地址  " << '\t' << "目的地址" << '\t' << "协议类型  " << "数据包数量" <<endl;
  for(pTemp = pHead; pTemp; pTemp = pTemp->pNext)
  {
   unsigned long dwSourTemp = pTemp->getSourIPAddr();
   unsigned long dwDestTemp = pTemp->getDestIPAddr();
   out << inet_ntoa(*(in_addr *)&(dwSourTemp)) << '\t';
   out << inet_ntoa(*(in_addr *)&(dwDestTemp)) << '\t';
   out << resetiosflags(ios::right) << setiosflags(ios::left)
    << setfill(' ') << setw(10) << pTemp->getProtocol_String()
    << resetiosflags(ios::left) << setiosflags(ios::right);
   out << pTemp->getCount() << endl;
  }
 }
 return out;
}

你可能感兴趣的:(求教求教!!)