线程在套接字c ++窗口中抛出错误时异常

时间:2011-09-08 13:26:14

标签: c++ windows sockets exception-handling

我正在使用socket在Windows中创建客户端服务器应用程序,我想在运行时从线程抛出异常,如果出现任何问题,但我收到了throw语句的错误。

    //create thread in cpp file
    CreateThread(NULL,0,startServer,this,0,NULL);

        //thread in header file
            static unsigned long __stdcall startServer(void *i_SocketTransportServer)
           {
                           ((SocketTransportServer*)i_SocketTransportServer)->StartServerThread(((SocketTransportServer *)i_SocketTransportServer)->m_socketServer);
                return 0;
           }

        //and StartServerThread is function called by thread
         // SocketTransportServer is inner class of  RMLThinTransport
            void RMLThinTransport::SocketTransportServer::StartServerThread(SOCKET i_socketServer)
            {
                m_socketAccept=NULL;
                while(true)
                {
                    Sleep(20);
                    if(m_canAcceptMore)
                    {
                        m_canAcceptMore=false;
                        if(!m_isRunning)
                        {
                                break;
                        }
                        try
                        {
                            m_socketAccept=accept(m_socketServer,NULL,NULL);
                            if(m_socketAccept==INVALID_SOCKET)
                            {
                                int lastError=WSAGetLastError();
                                closesocket(m_socketAccept);
                                                                                                                            SocketExceptions                                        
 exceptionInAcceptAtServer;
                                  exceptionInAcceptAtServer.detectErrorAccept(&lastError);

throw exceptionInAcceptAtServer;
                            }
                            else
                            {
                                //_LOG("Client connected",EventTypeInfo) ;
                                OutputDebugStringW(L"client connected.....");
                                /* If client connected then setClinetCout value 1 */
                                setClientCount(1);
                                m_ClientSockets.push_back(m_socketAccept);
                                CreateThread(NULL,0,receiveDataAtServer,this,0,NULL);
                            }

                        }
                        catch(SocketExceptions& i_exceptionInAcceptAtServer)
                        {   

                            /*OutputDebugStringW(L"Can't accept client In Exception. ."); */
    throw i_exceptionInAcceptAtServer;//getting runtime error from here

                        }
                    }

                }

            }

现在我想在服务器关闭时抛出错误,但我得到运行时错误。所以有什么办法让我可以在我的主函数中得到错误。但我是c ++的新手,所以请帮助我。错误是

enter image description here

1 个答案:

答案 0 :(得分:0)

抛出异常的代码不是问题;缺少任何代码来捕获异常是问题所在。应用程序正在终止,因为没有任何东西可以捕获您正在抛出的异常;你必须确保某些事情能够抓住它。你的startServer方法 - 线程程序 - 必须捕获异常,然后彻底退出线程。