服务器端的程序,在接收到客户端的消息后,立即开启线程处理数据。正常的日志显示类似:
接收到 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);
}
}

解决方案 »

  1.   

     EnterCriticalSection不是用来同步_beginthreadex,而应该用来同步WorkerThread内的输出
      

  2.   

    char buffer[20480]="";
    改成
    char* buffer = new char[20480];
    在线程中delete。
      

  3.   


    线程中关闭buffer ??
    请赐教
      

  4.   

    就是在线程中使用完buffer之后用delete释放,这是很常用的给线程传递参数的方法。
    你的代码中buffer是局部数据,将局部数据传给线程很容易出错,因为局部数据出了{}之后就释放了,这之后可能线程还需要访问该数据。
      

  5.   

    在线程中删除了,结果发现多接收了好多
    现在发送速率差不多每秒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;
            。。
    }
      

  6.   

    TCP协议不是每次send对应一次recv的,你在短时间内多次发送的数据会被重新组合,一次recv收到多次send的数据是很正常。
      

  7.   

    现在出现了一个新的问题,日志显示
    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*
      

  8.   

    我觉的现在是 buffer没有delete之前 新的线程又开始使用了这个buffer 这样内容就多了。
      

  9.   

    在线程里面开buffer,但不推荐,因为线程里开buffer占用稀少的栈空间.建议做一个线程链表管理类,在类里分配堆内存块,这样,各自处理自己的数据.可以看看线程序池方面的知识,看看人家是怎么处理的.
      

  10.   

    要不使用异步吧,个人觉的还不错
    if(WSAAsyncSelect(m_hSocket,m_hWnd,m_WM_WINSOCK,FD_READ|FD_WRITE|FD_CLOSE|FD_CONNECT)>0)
    {
    return -1;//AfxMessageBox("Error in select");
    }
      

  11.   

    结贴了。cnzdgs所说的方法时可以的,后面出的问题是,buff创建后没有清零引起的谢谢大家。