CAsyncSocket为何不触发OnReceive?
Atry 2006-08-07 10:19:18 这是一个UDP的客户端,代码如下,只有几行代码,问题在于OnReceive始终不被触发
当另外的代码调用CMySocket::Create();后,能够正常创建。然后向服务器发出请求,发出请求后,如果马上调用ReceiveFrom,代码中没有这样做,但是如果这样做的话,有可能能成功,也有可能不成功。而OnReceive中的代码始终不被运行。我不知道这是为什么
// MySocket.cpp : implementation file
//
#include "stdafx.h"
#include "MySocket.h"
// CMySocket
CMySocket::CMySocket()
:provider(NULL)
{
}
CMySocket::~CMySocket()
{
}
// CMySocket member functions
void CMySocket::Create()
{
CAsyncSocket::Create(0 , SOCK_DGRAM, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE);
ReqToken reqToken;
CAsyncSocket::SendTo(&reqToken, sizeof(reqToken),8000,_T("sz2.tencent.com"));
// 如果这里直接调用ReceiveFrom,则有可能能收到,也有可能收不到
}
void CMySocket::OnReceive(int nErrorCode)
{
//这里根本不会运行
char inBuf[1023];
int sockAddrLen = sizeof(SOCKADDR_IN);
SOCKADDR_IN sockAddr;
ReceiveFrom(inBuf, sizeof(inBuf), (SOCKADDR*)&sockAddr, &sockAddrLen);
CAsyncSocket::OnReceive(nErrorCode);
}
void CMySocket::OnConnect(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
CAsyncSocket::OnConnect(nErrorCode);
}
void CMySocket::OnSend(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
CAsyncSocket::OnSend(nErrorCode);
}
void CMySocket::OnClose(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
CAsyncSocket::OnClose(nErrorCode);
}
void CMySocket::OnAccept(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
CAsyncSocket::OnAccept(nErrorCode);
}