CAsyncSocket :: Close崩溃 [英] CAsyncSocket::Close Crashes

查看:418
本文介绍了CAsyncSocket :: Close崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我做一些客户端/服务器的东西在windows服务。

Hey, im doing some client/server stuff in a windows service. Pretty much new to this stuff.

我遇到的问题是,当我尝试通过服务管理器停止服务时,它崩溃了。我添加了一些MessageBoxes代码,以跟踪他们在哪里崩溃,我发现当它关闭侦听器套接字它崩溃!!!

The problem I'm encountering is that when I try to stop the service through Service Manager, it crashes. I added some MessageBoxes code, to trace where they are crashing and I found that when it closes the listener socket it crashes!!!

我试图运行服务作为一个控制台应用程序,并且由我自己调用称为 SERVICE__CONTROL__STOP 的函数,以便我可以重现该错误和调试容易。但它工作正常。 Windows服务只有在我通过服务管理器停止时才会崩溃

I tried to run the service as a console application, and by myself called the function which is called SERVICE__CONTROL__STOP event is received so that I may reproduce the bug and debug easily. But it is working fine. The windows service is only crashing when I stop it through Service Manager

以下是一些代码

主要功能

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;
}

服务处理程序回调函数

// 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
    // ..
    // ..
}

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 a connection is made (client connects to the server) and the client closes the connection and then the service is stopped nothing crashes. It only crashes if the socket is listening and no connection is accepted or the client doesnt closes the connection and the service is stopped :)

我猜它的清楚!

推荐答案

尝试添加: -

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

添加到您的主题或类初始化器。

to your thread or class initialiser.

这篇关于CAsyncSocket :: Close崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆