Home > other >  VC TCP server multithreaded programming problem
VC TCP server multithreaded programming problem

Time:10-03

Good god

I now do a TCP sever links with VC program, now need more links can be implemented, but after closing the TCP server in links, mistakes will happen


I found the TCP server to debug tracking, shut down, build threads continues to run, but I have no way to normal shut down or quit
Below set up to monitor multithreaded program
UINT _ListenThread (LPVOID param)
{
PDlg=(CMYQQDlg * param);
Int ret=pDlg - & gt; M_Server. InitAndListen (pDlg - & gt; M_Port);
If (ret==0)
{
: : AfxMessageBox (ret==0! "" );
PDlg - & gt; ResetListMsg (1);

ReceiveThread_S=: : AfxBeginThread (_ReceiveThread_S pDlg);
}

//continuously listens for
The WaitForSingleObject (pDlg - & gt; HPrepareEvent, 0);
ListenThread=: : AfxBeginThread (_ListenThread pDlg);

return 0;
}

Void CMYQQDlg: : OnListen ()
{
//TODO: Add your the control notification handler code here
ListenThread=: : AfxBeginThread (_ListenThread, this);
}



Don't close the program, an execution is not closed, collapse is
Void CMYQQDlg: : Close_TCP_Server ()
{

//TODO: Add your the control notification handler code here
//nCol=0;
//nConnectClients=0;

If (pDlg - & gt; M_Server m_hSocket!=NULL)
{
Closesocket (pDlg - & gt; M_Server. M_hSocket);//is turned off, can produce more than 204204204. The 204 IP bug
PDlg - & gt; M_Server. M_hSocket=NULL;
}
DWORD dwExitCode;
If (ReceiveThread_S!=NULL)
{
GetExitCodeThread (_ReceiveThread_S, & amp; DwExitCode);
: : AfxEndThread (dwExitCode, TRUE);//use this statement is turned off, program crashes, but the thread is not really close
ReceiveThread_S=NULL;
}
SuspendThread (_ListenThread);
SuspendThread (_ReceiveThread_S);
If (ListenThread!=NULL)
{

GetExitCodeThread (_ListenThread, & amp; DwExitCode);
ExitThread (dwExitCode);//use this statement is turned off, program crashes,
ListenThread=NULL;
}

}

  • Related