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. QTcpServer listen successfully,but without emiting newConnection
Forum Updated to NodeBB v4.3 + New Features

QTcpServer listen successfully,but without emiting newConnection

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 1.7k 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.
  • P Offline
    P Offline
    pinkie
    wrote on 14 Dec 2017, 12:33 last edited by VRonin
    #1

    Hi , I test QTcpServer of Qt5.3.1. The steps are following:

    m_pTCPServer = new QTcpServer(this);
    m_pTCPServer->setMaxPendingConnections(1);
    QObject::connect(m_pTCPServer,SIGNAL(newConnection()),this,SLOT(server_New_Connect()));
    if(m_pTCPServer->listen(QHostAddress::Any, m_localPort))
    {
    setCommStatus(CommunicationStatus::Connected);
    ret = true;
    }
    

    I can test the listening is OK with a tcp client connection, but the slot 'server_New_Connect' never be emitted.

    Anyone has already encounted this problem,pls give me some advice,thanks a lot.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 14 Dec 2017, 13:02 last edited by
      #2

      @pinkie said in QTcpServer listen successfully,but without emiting newConnection:

      QObject::connect(m_pTCPServer,SIGNAL(newConnection()),this,SLOT(server_New_Connect()));

      Can you check the return value of connect? the easiest way is to wrap it into Q_ASSUME: Q_ASSUME(QObject::connect(m_pTCPServer,SIGNAL(newConnection()),this,SLOT(server_New_Connect())));

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • M Offline
        M Offline
        mchinand
        wrote on 14 Dec 2017, 19:14 last edited by
        #3

        @pinkie said in QTcpServer listen successfully,but without emiting newConnection:

        I can test the listening is OK with a tcp client connection

        By this, do you mean that your client does a connectToHost() and a connected() signal is subsequently emitted by the client?

        Have you looked a the Fortune client/server examples?

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Pablo J. Rogina
          wrote on 14 Dec 2017, 22:58 last edited by
          #4

          @pinkie I lack context of where you're creating your QTcpServer object but the following code is working fine for me (Qt 5.9.0): basic Qt Widgets Application project with m_pTCPServer member added to MainWindow class :

          mainwindow.h

          #ifndef MAINWINDOW_H
          #define MAINWINDOW_H
          
          #include <QMainWindow>
          #include <QtNetwork/QTcpServer>
          
          namespace Ui {
          class MainWindow;
          }
          
          class MainWindow : public QMainWindow
          {
              Q_OBJECT
          
          public:
              explicit MainWindow(QWidget *parent = 0);
              ~MainWindow();
          
          public slots:
              void server_New_Connect();
          
          private:
              Ui::MainWindow *ui;
              QTcpServer* m_pTCPServer;
          };
          
          #endif // MAINWINDOW_H
          
          

          mainwindow.cpp

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          #include <QDebug>
          
          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
          
              m_pTCPServer = new QTcpServer(this);
              m_pTCPServer->setMaxPendingConnections(1);
              QObject::connect(m_pTCPServer, SIGNAL(newConnection()), this, SLOT(server_New_Connect()));
          
              if (m_pTCPServer->listen(QHostAddress::Any, 9999))
              {
                  qDebug() << "Server up and listening!";
              }
          }
          
          MainWindow::~MainWindow()
          {
              delete ui;
          }
          
          void MainWindow::server_New_Connect()
          {
              qDebug() << "New connection!";
          }
          

          program output when using command telnet localhost 9999

          Server up and listening!
          New connection!
          

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pinkie
            wrote on 15 Dec 2017, 03:28 last edited by
            #5

            Thanks all for your help. I find the point. The new QTcpServer... code is within run function of my thread. It works well if I move it outside. I should learn more about QTcpServer and QThread. Thank you.

            T 1 Reply Last reply 15 Dec 2017, 10:57
            1
            • P pinkie
              15 Dec 2017, 03:28

              Thanks all for your help. I find the point. The new QTcpServer... code is within run function of my thread. It works well if I move it outside. I should learn more about QTcpServer and QThread. Thank you.

              T Offline
              T Offline
              Taz742
              wrote on 15 Dec 2017, 10:57 last edited by
              #6

              @pinkie
              Mark this post a solved.

              Do what you want.

              1 Reply Last reply
              1
              • P Offline
                P Offline
                Pablo J. Rogina
                wrote on 15 Dec 2017, 17:13 last edited by
                #7

                @pinkie you may want to take a look at threaded fortune server example. And as @Taz742 mentioned, please don't forget to mark the post as solved.

                Upvote the answer(s) that helped you solve the issue
                Use "Topic Tools" button to mark your post as Solved
                Add screenshots via postimage.org
                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                T 1 Reply Last reply 15 Dec 2017, 17:46
                1
                • P Pablo J. Rogina
                  15 Dec 2017, 17:13

                  @pinkie you may want to take a look at threaded fortune server example. And as @Taz742 mentioned, please don't forget to mark the post as solved.

                  T Offline
                  T Offline
                  Taz742
                  wrote on 15 Dec 2017, 17:46 last edited by
                  #8

                  What happend when connection server&client is always open?

                  Do what you want.

                  1 Reply Last reply
                  0

                  8/8

                  15 Dec 2017, 17:46

                  • Login

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