哦,问题可能在那里: void Client::OnReceive(int nErrorCode) { // TODO: Add your specialized code here and/or call the base class
CSocket::OnReceive(nErrorCode); m_dlg->Serverdata(); } m_dlg->Serverdata(); 一行应该放在CSocket::OnReceive(nErrorCode); 的前面,也就是如下: void Client::OnReceive(int nErrorCode) { // TODO: Add your specialized code here and/or call the base class
int buflen=pSocket->Receive(serverbuf,1024,0);//出错
你是不是采用了多线程进行开发的?
声明、赋值的部分
class Client : public CSocket
{
// Attributes
public:// Operations
public:
Client(CNtDlg* pdlg);
virtual ~Client();
CNtDlg* m_dlg;
virtual void OnConnect(int nErrorCode);
virtual void OnReceive(int nErrorCode);
// Overrides
public:
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(Client)
public: //}}AFX_VIRTUAL // Generated message map functions
//{{AFX_MSG(Client)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG// Implementation
protected:
};//client.cpp#include "NtDlg.h"
/////////////////////////////////////////////////////////////////////////////
// ClientClient::Client(CNtDlg* pdlg)
{
m_dlg=pdlg;
}Client::~Client()
{
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(Client, CAsyncSocket)
//{{AFX_MSG_MAP(Client)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0/////////////////////////////////////////////////////////////////////////////
// Client member functionsvoid Client::OnConnect(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
//CAsyncSocket::OnConnect(nErrorCode);
}void Client::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
CSocket::OnReceive(nErrorCode);
m_dlg->Serverdata();
}
char serverbuf[1024];
CString m_ip;
int m_port;
int Trycount;
void Serverdata();
if (!pSocket->Create())
{
int nResult = GetLastError();
WSASetLastError(nResult);
delete pSocket;
pSocket = NULL;
//AfxMessageBox("套接字创建失败");
//return 1;
}
int nResult = GetLastError();
WSASetLastError(nResult);
m_ip="210.51.0.33";
m_port=5432;
//MessageBox(m_ip,"ip");
while(!pSocket->Connect(m_ip,m_port))
{
if(Trycount>2)
{
delete pSocket;
pSocket=NULL;
//MessageBox("连接服务器失败");
return 1;
}
Trycount++;
}
char strsendbuf[1234];
strsend=senddata;
int ns=pSocket->Send(strsendbuf,fulllen,0);
void Client::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
CSocket::OnReceive(nErrorCode);
m_dlg->Serverdata();
} m_dlg->Serverdata(); 一行应该放在CSocket::OnReceive(nErrorCode);
的前面,也就是如下:
void Client::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
m_dlg->Serverdata();
CSocket::OnReceive(nErrorCode);
}
char serverbuf[1024];
该为
char serverbuf[1025];
int nResult = GetLastError();
WSASetLastError(nResult);
CAsyncSocket::recv()的时候出错
而且,lpBuf参数无效