OnReceive不能被触发
g_yxh 2003-11-26 01:53:22 代码:
ULONG startIP = inet_addr("192.168.0.1");
ULONG mask = inet_addr("255.255.255.0");
CNBTSTAT nbtstat;
for (ULONG nRemoteAddr = startIP;
(nRemoteAddr & mask) == (startIP & mask);
nRemoteAddr = htonl( ntohl( nRemoteAddr ) + 1 ) )
{
nbtstat.SendMessage(nRemoteAddr);
}
有两个类:CNBTSTAT和CUDP,分别如下:
nbtstat.cpp:-------------------------------------------------
#include "stdafx.h"
#include "NBTSTAT.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define destPORT 137 //nbtstat name port
#define myPORT 1234
CNBTSTAT* pNbtstat;
BYTE bs[50]={0x0,0x00,0x0,0x10,0x0,0x1,0x0,0x0,0x0,0x0,0x0,0x0,0x20,0x43,0x4b,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x41,0x0,0x0,0x21,0x0,0x1};
CNBTSTAT::CNBTSTAT()
{
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
}
pNbtstat=this;
if(!m_UDPSocket.Create(myPORT,SOCK_DGRAM))
{
AfxMessageBox("Failed Create Socket");
}
}
void CNBTSTAT::SendMessage(ULONG nRemoteAddr)
{
pNbtstat->m_UDPSocket.SendTo((void*)bs,50,destPORT,inet_ntoa(nRemoteAddr),0);//inet_ntoa()是自己写的一个函数,把ulong转换成char[]
Sleep(1000);
}
void CNBTSTAT::OnReceive()
{
/////此处代码省略
}
nbtstat.h:--------------------------------------------------------
// NBTSTAT.h : main header file for the NBTSTAT application
//
#if !defined(AFX_NBTSTAT_H__DE6CDDC5_A43B_11D5_A956_0050BA0F0366__INCLUDED_)
#define AFX_NBTSTAT_H__DE6CDDC5_A43B_11D5_A956_0050BA0F0366__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "stdafx.h"
#include "resource.h" // main symbols
#include "UDP.h" // Added by ClassView
/////////////////////////////////////////////////////////////////////////////
// CNBTSTATApp:
// See NBTSTAT.cpp for the implementation of this class
//
class CNBTSTAT
{
public:
CNBTSTAT();
void OnReceive();
CUDP m_UDPSocket;
void SendMessage(ULONG nRemoteAddr);
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_NBTSTAT_H__DE6CDDC5_A43B_11D5_A956_0050BA0F0366__INCLUDED_)
udp.cpp:--------------------------------------------------------------
// UDP.cpp : implementation file
//
#include "stdafx.h"
#include "NBTSTAT.h"
#include "UDP.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CUDP
//-----------------------------------------------
extern CNBTSTAT* pNbtstat;
//-----------------------------------------------
CUDP::CUDP()
{
}
CUDP::~CUDP()
{
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CUDP, CSocket)
//{{AFX_MSG_MAP(CUDP)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CUDP member functions
void CUDP::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
pNbtstat->OnReceive();
CSocket::OnReceive(nErrorCode);
}
udp.h:--------------------------------------------------------------
#if !defined(AFX_UDP_H__DE6CDDCF_A43B_11D5_A956_0050BA0F0366__INCLUDED_)
#define AFX_UDP_H__DE6CDDCF_A43B_11D5_A956_0050BA0F0366__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// UDP.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CUDP command target
class CUDP : public CSocket
{
// Attributes
public:
// Operations
public:
CUDP();
virtual ~CUDP();
// Overrides
public:
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CUDP)
public:
virtual void OnReceive(int nErrorCode);
//}}AFX_VIRTUAL
// Generated message map functions
//{{AFX_MSG(CUDP)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
// Implementation
protected:
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_UDP_H__DE6CDDCF_A43B_11D5_A956_0050BA0F0366__INCLUDED_)
结果包发出去了,也收得到(用sniffer看),但OnReceive(int)不能被执行,是怎么回事呢?