Home > Software engineering >  How to stop the MFC Socket listening?
How to stop the MFC Socket listening?

Time:01-21

CWinThread * SocketThread;//child dialogs private variable

My logic is this:
When opening the dialog, call AfxBeginThread (& amp; (Optimizer: : server_thd), 0); Creating a thread,

SocketThread=AfxBeginThread (& amp; (Optimizer: : server_thd), 0);//thread begin

I use tinder network analysis, can see already to start listening to the corresponding port,

Server_thd is a class of the static function and use of the socket and listen_sock are static variables,

The main binding and monitoring were as follows:
: : bind (listen_sock, (struct sockaddr *) & amp; Local_addr, sizeof (SOCKADDR_IN))
The sock=accept (listen_sock, (struct sockaddr *) & amp; Client_addr, & amp; IaddrSize)


When closing the dialog box, I want to close my listening, I invoke the
Shutdown (sock, SD_BOTH);
Closesocket (sock);
Shutdown (listen_sock SD_BOTH);
Closesocket (listen_sock);
The TerminateThread (SocketThread - & gt; M_hThread, 0);
But viewing from the tinder, or in the listener port, until the whole software close to stop listening, how do I stop listening?

CodePudding user response:

1 closed before setting a breakpoint, trial run and see whether the want to logic order
2 threads do not recommend forced interruption, easy resource leaks, it is recommended to use mark way waiting for events such as
3 after the Socket is closed, the system in order to ensure the operation is not used to this immediately behind the connection and there will be a waiting time, pay attention to the

CodePudding user response:

refer to the second floor zgl7903 response:
1 closed before setting a breakpoint, trial run and see whether the want to logic order
2 threads do not recommend forced interruption, easy resource leaks, it is recommended to use mark way waiting for events such as
3 after the Socket is closed, the system in order to ensure the operation is not used to this immediately behind the connection and there will be a waiting time, pay attention to the


Set a breakpoint, is performed according to the logic, and the number of threads is less 1
Waiting for a long time, there are still listening
  • Related