Server thread inside a qt class (need mutex?)

前端 未结 2 462
渐次进展
渐次进展 2021-01-16 06:22

I made this server class that starts a thread when new connection comes in. It works ok with some cases, but it\'s not very stable. I am trying to solve where it breaks. My

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-16 06:59

    I got my old "the wrong way to do it" code to work. I guess this part was where the error was:

    //removed
    tcpSocket.disconnectFromHost();
    tcpSocket.waitForDisconnected();
    emit finished();
    

    ...

    #include 
    #include 
    #include "MeshServer.hh"
    #include 
    #include "TableView.hh"
    
    using namespace  std;
    
    FortuneServer::FortuneServer(QObject *parent)
    : QTcpServer(parent)
    {
    
    }
    
    void FortuneServer::procesServerString(string serverString){
    
    emit procesServerStringToParent(serverString);
    
    }
    void FortuneServer::getStringToThread(string serverString){
    
    emit getStringToThreadSignal(serverString);
    
    }
    
    void FortuneServer::incomingConnection(int socketDescriptor)
    {
    
    
    FortuneThread *serverthread = new FortuneThread(socketDescriptor, this);
    //connect(&serverthread, SIGNAL(finished()), &serverthread, SLOT(deleteLater()));
    
    
    QThread* thread = new QThread;
    
    serverthread->moveToThread(thread);
    
    
    
    
    connect(serverthread, SIGNAL(fromThreadString(string)), this, SLOT(procesServerString(string)));
    connect(this, SIGNAL(getStringToThreadSignal(string)), serverthread, SLOT(sendString(string)));
    connect(serverthread, SIGNAL(finished()), thread, SLOT(quit()));
    connect(serverthread, SIGNAL(finished()), serverthread, SLOT(deleteLater()));
    connect(serverthread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    
    thread->start();
    
    }
    
    
    
    FortuneThread::FortuneThread(int socketDescriptor, QObject *parent): QObject(parent),   socketDescriptor(socketDescriptor)
    
    {
    if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
        emit error(tcpSocket.error());
        cout<<"socket error"<

提交回复
热议问题