Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved]QTcpServer can't catch the SIGNAL "newConnection"

[solved]QTcpServer can't catch the SIGNAL "newConnection"

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BBFAG3
    wrote on last edited by
    #1

    I run this TcpServer in a new thread, and I send a signal from main thread, it works.BUT! when a client connect to this server, the signal "newConnection" was not been called.

    This is my code, thx a lot!

    //main.cpp
    @#include <QApplication>
    #include"QThread"
    #include"QObject"
    #include"QTextStream"
    #include"kserver.h"
    #include"testclass.h"
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    qDebug()<<"main thread:"<<QThread::currentThreadId();

    QThread serverThread;
    KServer server(&a);
    testClass t;
    server.moveToThread(&serverThread);
    
    QObject::connect(&server.KLServer,SIGNAL(newConnection()),&server,SLOT(newConnect()));
    
    QObject::connect(&t,SIGNAL(t()),&server,SLOT(newConnect()));
    serverThread.start();
    
    t.emits();//This is my test signal,this signal was work.
    
    QTextStream cin(stdin);
    
    QString iptCmd;
    while(1)
    {
        //qDebug()<<server.KLServer.isListening();
        cin>>iptCmd;
    }
    return a.exec&#40;&#41;;
    

    }
    @

    //kerver.cpp
    @#include "kserver.h"

    #include"QDebug"
    KServer::KServer(QApplication *myApp, QObject *parent) :
    QObject(parent)
    {
    myApp->processEvents();//I saw a post on StackOverflow, he said this will make it work, but it doesn't....
    KLServer.listen(QHostAddress::Any,9825);
    qDebug()<<"Server Thread"<<QThread::currentThreadId();
    qDebug()<<"listening";

    }

    void KServer::receiveMsg()
    {
    qDebug()<<"RM";
    qDebug()<<QThread::currentThreadId();
    }

    void KServer::newConnect()//never step into this function..
    {
    qDebug()<<"Server Thread"<<QThread::currentThreadId();
    }
    @

    "testClass" only have a signal "t()" and a function to emit the signal, other headers only use to declare.

    Thanks a LOOOOOOT!!!!!!!

    1 Reply Last reply
    0
    • B Offline
      B Offline
      BBFAG3
      wrote on last edited by
      #2

      BTW, I'm sure the client connect to the server successfully, because if I close server side before client side, it will notice:The remote host closed the connection

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gyll
        wrote on last edited by
        #3

        You have this:
        @ while(1)
        {
        //qDebug()<<server.KLServer.isListening();
        cin>>iptCmd;
        }
        return a.exec();@

        This means your main application will never execute a.exec(), i.e. it will never enter the event loop

        1 Reply Last reply
        0
        • B Offline
          B Offline
          BBFAG3
          wrote on last edited by
          #4

          [quote author="gyll" date="1392737140"]You have this:
          @ while(1)
          {
          //qDebug()<<server.KLServer.isListening();
          cin>>iptCmd;
          }
          return a.exec();@

          This means your main application will never execute a.exec(), i.e. it will never enter the event loop[/quote]

          Thanks a lot....I really make a stupid mistake.....

          1 Reply Last reply
          0
          • G Offline
            G Offline
            gyll
            wrote on last edited by
            #5

            Just out of curiosity: what kind of app are you trying to build?

            1 Reply Last reply
            0
            • B Offline
              B Offline
              BBFAG3
              wrote on last edited by
              #6

              [quote author="gyll" date="1392737657"]Just out of curiosity: what kind of app are you trying to build?[/quote]

              Eh..A game's server side..

              1 Reply Last reply
              0
              • A Offline
                A Offline
                ankursaxena
                wrote on last edited by
                #7

                Ok .. If your problem is solved.. then please tag it is as a [solved]

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved