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
QtWS25 Last Chance

QTcpServer listen successfully,but without emiting newConnection

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 1.6k Views
  • 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 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
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on 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 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
        • Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on 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 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.

            Taz742T 1 Reply Last reply
            1
            • P pinkie

              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.

              Taz742T Offline
              Taz742T Offline
              Taz742
              wrote on last edited by
              #6

              @pinkie
              Mark this post a solved.

              Do what you want.

              1 Reply Last reply
              1
              • Pablo J. RoginaP Offline
                Pablo J. RoginaP Offline
                Pablo J. Rogina
                wrote on 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

                Taz742T 1 Reply Last reply
                1
                • Pablo J. RoginaP Pablo J. Rogina

                  @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.

                  Taz742T Offline
                  Taz742T Offline
                  Taz742
                  wrote on last edited by
                  #8

                  What happend when connection server&client is always open?

                  Do what you want.

                  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