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)不能被执行,是怎么回事呢?
...全文
155 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jollyja 2003-11-27
  • 打赏
  • 举报
回复
void CMySocket::OnAccept(int nErrorCode)
{
CSocket::Accept( *sRcv );
CSocket::OnAccept(nErrorCode);
}
你肯定没有重载这个函数,没有accept客户端的连接,因为如果你要receive的话,要用accept取得的socket号进行receive,而不是用listen的socket号进行receive。
g_yxh 2003-11-27
  • 打赏
  • 举报
回复
是void CUDP::OnReceive(int nErrorCode)没有执行,然后当然void CNBTSTAT::OnReceive()也就没有执行
LuckFox 2003-11-26
  • 打赏
  • 举报
回复
怎么多代码,怎么看呀!!
zjlgigi 2003-11-26
  • 打赏
  • 举报
回复
是哪一个OnReceive没有执行?

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧