CAsyncSocket :: Close Crashes

Hey, I am doing some client / server things in a windows service. Pretty much new for this.

The problem I am facing is that when I try to stop the service through Service Manager, it crashes. I added MessageBoxes code to track where they crash, and I found that when it closes the listener socket, it resets !!!

I tried to start the service as a console application and myself called a function called the SERVICE__CONTROL__STOP event so that I could easily reproduce the error and debug it. But it works fine. Windows service only crashes when I stop it through Service Manager

Here is the code

Main function

int main(int argc, char* argv[]) { // Create the service object CTestService CustomServiceObject; if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { std::cerr << "MFC failed to initialize!" << std::endl; return 1; } // Parse for standard arguments (install, uninstall, version etc.) if (! CustomServiceObject.ParseStandardArgs(argc, argv)) { // StartService() calls ::StartServiceCtrlDispatcher() // with the ServiceMain func and stuff CustomServiceObject.StartService(); } // When we get here, the service has been stopped return CustomServiceObject.m_Status.dwWin32ExitCode; } 

Service Handler Callback Function

 // static member function (callback) to handle commands from the // service control manager void CNTService::Handler(DWORD dwOpcode) { // Get a pointer to the object CNTService* pService = m_pThis; pService->DebugMsg("CNTService::Handler(%lu)", dwOpcode); switch (dwOpcode) { case SERVICE_CONTROL_STOP: // 1 pService->SetStatus(SERVICE_STOP_PENDING); pService->OnStop(); // .. // .. // other event handling // .. // .. } 

function OnStop ()

 void CTestService::OnStop() { m_sListener.ShutDown(2); m_sConnected.ShutDown(2); MessageBox(NULL, "After Shutdown", NULL, IDOK); m_sConnected.Close(); MessageBox(NULL, "Closed connected socket", NULL, IDOK); // crashes here when I try to stop through service manager // but if I run as console application works fine and terminates successfully m_sListener.Close(); MessageBox(NULL, "Closed listener socket", NULL, IDOK); ::PostThreadMessage(m_dwThreadID, WM_QUIT, NULL, NULL); MessageBox(NULL, "After PostThreadMessage", NULL, IDOK); } 

EDIT: If the connection is completed (the client connects to the server), and the client closes the connection, and then the service stops, nothing will work. It only fails if the socket is listening and the connection is not accepted, or the client does not close the connection and the service is stopped :)

I think it is clear!

0
source share
2 answers

Try to add: -

  WSADATA data; if(!AfxSocketInit(&data)) AfxMessageBox("Failed to Initialize Sockets",MB_OK| MB_ICONSTOP); 

for your stream or class initializer.

+1
source

The problem is that you are most likely using a socket from multiple threads. Multiple threads and CAsyncSocket do not mix - in fact, as noted in the documentation.

Usually you push the socket into your own workflow, then you will signal it when you need it.

-1
source

All Articles