这段代码没有改动过: // AsyncClient.cpp : Defines the class behaviors for the application. //#include "stdafx.h" #include "AsyncClient.h" #include "AsyncClientDlg.h"#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif///////////////////////////////////////////////////////////////////////////// // CAsyncClientAppBEGIN_MESSAGE_MAP(CAsyncClientApp, CWinApp) //{{AFX_MSG_MAP(CAsyncClientApp) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG ON_COMMAND(ID_HELP, CWinApp::OnHelp) END_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////// // CAsyncClientApp constructionCAsyncClientApp::CAsyncClientApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance }///////////////////////////////////////////////////////////////////////////// // The one and only CAsyncClientApp objectCAsyncClientApp theApp;///////////////////////////////////////////////////////////////////////////// // CAsyncClientApp initializationBOOL CAsyncClientApp::InitInstance() { if (!AfxSocketInit()) { AfxMessageBox(IDP_SOCKETS_INIT_FAILED); return FALSE; } AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need.#ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif CAsyncClientDlg dlg; m_pMainWnd = &dlg; int nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: Place code here to handle when the dialog is // dismissed with OK } else if (nResponse == IDCANCEL) { // TODO: Place code here to handle when the dialog is // dismissed with Cancel } // Since the dialog has been closed, return FALSE so that we exit the // application, rather than start the application's message pump. return FALSE; }
我是这样的
#define maxlength 1000000
memset(pBuffer,0,maxlength)
还是不行,问题可嫩不在这个地方!
#define MAXLENGTH 1000000
BYTE *pBuffer;
BYTE *pTemp;pBuffer = new BYTE[MAXLENGTH];
memset(pBuffer,0,MAXLENGTH);
pTemp=pBuffer;
还是不行啊!
BYTE *pTemp;
int nReceived;
char buf[1000];//缓冲区的大小如果太大,就可能非法操作
memset(buf,0,sizeof(buf));
nReceived=sock.Receive((BYTE *)buf,sizeof(buf));
memcpy(pBuffer,buf,nReceived);
pTemp = pTemp + nReceived;
Count = Count - nReceived;
if(Count < 100)
{//保存操作}
我的本意是要接收很大的数据,然后进行分次保存,避免缓冲过大,这个地方应该没有错吧!
{//保存操作}
我的本意是要接收很大的数据,然后进行分次保存,避免缓冲过大,这个地方应该没有错吧!
应该是在这个地方出错了,报出来的这个错误的原因是由于你在操作过程当中,对这个缓冲区的数据进行了修改,比如读取了缓冲区中的数据或者是再把数据写入了缓冲区,而你在对这个缓冲进行操作的时候,没有控制好数据同步的问题,致便指针指向的缓冲区地址不对,因此出错了。再检查一下你的这部分代码。
{
//save the file
HANDLE m_hFile;
m_hFile=CreateFile"c:\\zhubo.dat",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(m_hFile==INVALID_HANDLE_VALUE)
{
DWORD DisError= GetLastError();
CString str;
str.Format("保存时出错,错误号%u%u",HIWORD(DisError),LOWORD(DisError));
AfxMessageBox(str);
}
int ByteWriten;
BOOL wError=WriteFile(m_hFile,pBuffer,(MAXLENGTH - Count),(unsigned long *)&ByteWriten,NULL);
if(wError==FALSE)
{
MessageBox("写失败");
}
memset(pBuffer,0,MAXLENGTH);
Count=MAXLENGTH;
CloseHandle(m_hFile);
//save the file
}
应该是
if(Count < 1000)
如果nReceived=MAX-200
就有
Count = 200
下一次收 有可能
nReceived>MAXmemcpy(pBuffer,buf,nReceived);报错
发现如下的问题:
我的 CAsyncClientDlg dlg;
m_pMainWnd = &dlg;
发现地址是ox0083fbe4
但是我的构造函数
CAsyncClientDlg::CAsyncClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAsyncClientDlg::IDD, pParent)的地址也是ox0083fbe4,是不是这个地方冲突了,要如何解决,不知道我说清楚了没有?
请指点?
// AsyncClient.cpp : Defines the class behaviors for the application.
//#include "stdafx.h"
#include "AsyncClient.h"
#include "AsyncClientDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CAsyncClientAppBEGIN_MESSAGE_MAP(CAsyncClientApp, CWinApp)
//{{AFX_MSG_MAP(CAsyncClientApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CAsyncClientApp constructionCAsyncClientApp::CAsyncClientApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}/////////////////////////////////////////////////////////////////////////////
// The one and only CAsyncClientApp objectCAsyncClientApp theApp;/////////////////////////////////////////////////////////////////////////////
// CAsyncClientApp initializationBOOL CAsyncClientApp::InitInstance()
{
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
} AfxEnableControlContainer(); // Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif CAsyncClientDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
} // Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
另外的代码:
...public:
BYTE *pBuffer;
BYTE *pTemp;
int Count;
...
/*初始化操作*/
pBuffer = new BYTE[MAXLENGTH];
memset(pBuffer,0,MAXLENGTH); pTemp=pBuffer;
Count=MAXLENGTH;... int nReceived;
char buf[1000];//缓冲区的大小如果太大,就可能非法操作
memset(buf,0,sizeof(buf)); nReceived=sock.Receive((BYTE *)buf,sizeof(buf)); memcpy(pTemp,buf,nReceived); pTemp = pTemp + nReceived; Count = Count - nReceived; if(Count < 1000)
{
if (g_bSave==TRUE)
{
//save the file
HANDLE m_hFile;
m_hFile=CreateFile("c:\\zhubo.dat",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if(m_hFile==INVALID_HANDLE_VALUE)
{
DWORD DisError= GetLastError();
CString str;
str.Format("保存时出错,错误号%u%u",HIWORD(DisError),LOWORD(DisError));
AfxMessageBox(str);
} int ByteWriten;
BOOL wError=WriteFile(m_hFile,pBuffer,(MAXLENGTH - Count),(unsigned long *)&ByteWriten,NULL);
if(wError==FALSE)
{
MessageBox("写失败");
} memset(pBuffer,0,MAXLENGTH);
Count=MAXLENGTH; CloseHandle(m_hFile);
//save the file
}
}最好还有一个Delete,就这样!