服务器端的程序,在接收到客户端的消息后,立即开启线程处理数据。正常的日志显示类似:
接收到 abc
thread buff abc
现在的日志是
接收到 abc
接收到 abcabc
thread buff abcabc
thread buff abcabc这样就接收的就多了while (1)
{ char *pData;
int nRet; //while (!bEndingFound) //if ((nRet=psock->Recv(&pData,&cBytesRead,30000000))==RET_OK) //wait for 30 sec,otherwise discard
if ((nRet=psock->Recv(&pData))==RET_OK)
{
EnterCriticalSection( &g_csFile );
char buffer[20480]="";
memcpy(buffer,pData,strlen(pData));
HANDLE hThread;
if ((hThread=(HANDLE)_beginthreadex(NULL,0,WorkerThread,(void*)buffer,0,0))==0)
{
KTRACELN(LOG_WARNING,"_beginthreadex() for WorkerThread failed !");
}
else
{
CloseHandle(hThread);
}
Sleep(10);
LeaveCriticalSection( &g_csFile);
}
}
接收到 abc
thread buff abc
现在的日志是
接收到 abc
接收到 abcabc
thread buff abcabc
thread buff abcabc这样就接收的就多了while (1)
{ char *pData;
int nRet; //while (!bEndingFound) //if ((nRet=psock->Recv(&pData,&cBytesRead,30000000))==RET_OK) //wait for 30 sec,otherwise discard
if ((nRet=psock->Recv(&pData))==RET_OK)
{
EnterCriticalSection( &g_csFile );
char buffer[20480]="";
memcpy(buffer,pData,strlen(pData));
HANDLE hThread;
if ((hThread=(HANDLE)_beginthreadex(NULL,0,WorkerThread,(void*)buffer,0,0))==0)
{
KTRACELN(LOG_WARNING,"_beginthreadex() for WorkerThread failed !");
}
else
{
CloseHandle(hThread);
}
Sleep(10);
LeaveCriticalSection( &g_csFile);
}
}
改成
char* buffer = new char[20480];
在线程中delete。
线程中关闭buffer ??
请赐教
你的代码中buffer是局部数据,将局部数据传给线程很容易出错,因为局部数据出了{}之后就释放了,这之后可能线程还需要访问该数据。
现在发送速率差不多每秒300条。
socket有时会接收到多条。
unsigned __stdcall WorkerThread( void* pArguments )
{
char * pData = (char *) pArguments;
char * msgBuffer;
int n = strlen(pData);
msgBuffer = (char*) malloc (n+1);
memcpy(msgBuffer,pData,n);
delete [] pArguments;
。。
}
10/30 13:30:02 ( 2760: 3764) time --- 2681703 Recv from 127.0.0.1/19462 :
$222222222222222222222222*$222222222222222222222222*
10/30 13:30:02 [DEBUG] ( 2760: 3268) in workerthread --$222222222222222222222222*$222222222222222222222222*
10/30 13:30:02 [DEBUG] ( 2760: 3268) frame nums = 2
10/30 13:30:02 [DEBUG] ( 2760: 3268) Write to file success: $222222222222222222222222*
10/30 13:30:02 [DEBUG] ( 2760: 3268) Write to file success: $222222222222222222222222*
10/30 13:30:02 ( 2760: 3764) time --- 2681734 Recv from 127.0.0.1/19462 :
$222222222222222222222222*
10/30 13:30:02 ( 2760: 3764) time --- 2681734 Recv from 127.0.0.1/19462 :
$222222222222222222222222*
10/30 13:30:02 [DEBUG] ( 2760: 3012) in workerthread --$222222222222222222222222*$222222222222222222222222* //出问题的地方,应该是一个就对了
10/30 13:30:02 [DEBUG] ( 2760: 3012) frame nums = 2
10/30 13:30:02 [DEBUG] ( 2760: 1268) in workerthread --$222222222222222222222222*
10/30 13:30:02 [DEBUG] ( 2760: 1268) frame nums = 1
10/30 13:30:02 [DEBUG] ( 2760: 3012) Write to file success: $222222222222222222222222*10/30 13:30:02 [DEBUG] ( 2760: 1268) Write to file success: $222222222222222222222222*
10/30 13:30:02 [DEBUG] ( 2760: 3012) Write to file success: $222222222222222222222222*
if(WSAAsyncSelect(m_hSocket,m_hWnd,m_WM_WINSOCK,FD_READ|FD_WRITE|FD_CLOSE|FD_CONNECT)>0)
{
return -1;//AfxMessageBox("Error in select");
}