Home > Back-end >  Other threads properly closed blocking Socket threads?
Other threads properly closed blocking Socket threads?

Time:09-22

UINT DataRecvThreadProc (LPVOID lpParam)
{
* aDlg=C tools Dlg (C *) tool Dlg lpParam;//get the main dialogue window handle!

int len;
Cstrings STR.
Extern struct _TestNumRange TestNumRange;

if (! AfxSocketInit ())
{
AfxMessageBox (IDP_SOCKETS_INIT_FAILED);
return 1;
}

Int nPort=TestNumRange. PortID;;

CSocket aSocket;
CSocket serverSocket.

if (! ASocket. Socket ())
{
AfxMessageBox (_T (" create a socket error!" ));
return 1;
}

BOOL bOptVal=TRUE;
Int bOptLen=sizeof (BOOL);

ASocket. SetSockOpt (SO_REUSEADDR, (void *) & amp; BOptVal bOptLen, SOL_SOCKET);


if (! ASocket. Bind (nPort))
{
AfxMessageBox (_T (" binding port failure!" ));
return 1;
}

if (! ASocket. Listen (5))
{
AfxMessageBox (_T (" listening failed!" ));
return 1;
}
While (m_connect)
{

if (! ASocket. Accept (serverSocket))
{
continue;
}
The else
{
Unsigned char szRecvMsg [17412]={0};
While (1)
{
Len=serverSocket. The Receive (szRecvMsg, 17412);
if (! M_connect) break;
If (len==0 | | len==1)
{
M_connect=false; Recv_Signal=false;
If (Recv_Signal) break;
ServerSocket. Close ();
ASocket. Close ();
ADlg - & gt; M_pThread=NULL;//dropped the pointer NULL will not go wrong here?
AfxMessageBox (_T (" probe drops!" ));
return 0;
}
If (Recv_Signal)
{
Global_CriticalSection. The Lock ();
Frashfifo=true;
SignalFIFO. Input (szRecvMsg, len);
Global_CriticalSection. Unlock ();
}
}
ServerSocket. Close ();
}
}

ASocket. Close ();

return 0;
}




On the interface has two buttons, the start button is on the worker thread, and end button I am:


M_connect=false; Recv_Signal=false;

: : WaitForSingleObject (m_pThread - & gt; M_hThread, 1000);

M_pThread=NULL;

return;
But so close, will go wrong in the case of a:
When the worker thread blocking in
Len=serverSocket. The Receive (szRecvMsg, 17412);
When, if I press the start button and within 1000 ms of the Socket is not received new signal, the worker threads cannot close, but only the pointer is NULL,
When I work and open thread, there will be two Socket listening to the same port, this will lead to serious consequences,,,
Therefore, I want to know in this case I want to how to implement the UI to control the switch of worker threads,
Note: in the practical use, serverSocket. The Receive will received several: a bag 5 ms, a minute a package, so if using CEvent, I don't know how design can guarantee should not replace the can realize the switch control of the worker threads at the same time,

CodePudding user response:

Don't sink,,
I found a kind of method is:
if (! ASocket. Accept (serverSocket))
Thread to try catch structure, then the interface need to close this thread, to create a thread interrupts, so as to exit, the problem is
I didn't find it in the MFC inside thread interrupt method,,

CodePudding user response:

Using asynchronous socket, do not use blocking socket,
Blocking socket can only oneself give oneself send a socket message, otherwise,,,

CodePudding user response:

Disconnected, resulting from a Socket

CodePudding user response:

You put 1000 into infinite wait for no
  • Related