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. QTCP run 2 client on main thread, for 2 server, separate QAbstractSocket signal

QTCP run 2 client on main thread, for 2 server, separate QAbstractSocket signal

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 852 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.
  • gfxxG Offline
    gfxxG Offline
    gfxx
    wrote on last edited by
    #1

    Sorry at all but quite new in soket, so before discower the weel, i prefer ask if is possible run 2 different client on same mainwindows or same thared .... server is different and work on different ip adress .... but now when start one client other close because disconnected signal .... when my code is write for persistent connection ..... seems QAbstractSocket signal was valid for one client and for other too at same time .... so think problem was separate QAbstractSocket signal about error and state for better management of void.

    ANy suggestion? is possible to reach these things 2 client os same thread?

    bkt

    jsulmJ 1 Reply Last reply
    0
    • gfxxG gfxx

      Sorry at all but quite new in soket, so before discower the weel, i prefer ask if is possible run 2 different client on same mainwindows or same thared .... server is different and work on different ip adress .... but now when start one client other close because disconnected signal .... when my code is write for persistent connection ..... seems QAbstractSocket signal was valid for one client and for other too at same time .... so think problem was separate QAbstractSocket signal about error and state for better management of void.

      ANy suggestion? is possible to reach these things 2 client os same thread?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @gfxx said in QTCP run 2 client on main thread, for 2 server, separate QAbstractSocket signal:

      i prefer ask if is possible run 2 different client on same mainwindows or same thared

      Yes, it is.

      "but now when start one client other close because disconnected signal" - please show your code. Also, does the server support more than one connection at the same time?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      gfxxG 1 Reply Last reply
      1
      • jsulmJ jsulm

        @gfxx said in QTCP run 2 client on main thread, for 2 server, separate QAbstractSocket signal:

        i prefer ask if is possible run 2 different client on same mainwindows or same thared

        Yes, it is.

        "but now when start one client other close because disconnected signal" - please show your code. Also, does the server support more than one connection at the same time?

        gfxxG Offline
        gfxxG Offline
        gfxx
        wrote on last edited by gfxx
        #3

        @jsulm quite simple .... server is done by qt example .... and client is these:

        /* on mainwindows.h */
        
        private: 
            QTcpSocket *tcpSocket;
            QDataStream in;
            QTcpSocket *tcpSocket1;
            QDataStream in1;
        
        /* on mainwindows.h   end  */
        
        /* on mainwindows  */
        tcpSocket = new QTcpSocket(this);
            in.setDevice(tcpSocket);
            in.setVersion(QDataStream::Qt_5_15);
        
           tcpSocket->connectToHost("19x.16x.50.xxx", xx050); //(ipHost, ipPort.toShort()); //ipPort.toShort());
            
           if (tcpSocket->waitForConnected()){
                        qDebug("Connected!");
           }
            connect(tcpSocket,&QAbstractSocket::disconnected,[]()->void{qDebug("Disconnected");});
        
            connect(tcpSocket, &QIODevice::readyRead, this, &MainWindow::readFortune);
            connect(tcpSocket, &QAbstractSocket::stateChanged, this, &MainWindow::sokState);
            connect(tcpSocket, &QAbstractSocket::errorOccurred, this, &MainWindow::displayError);
        
        /* end */ 
        
        
        void MainWindow::startSokconnection1(){   /* a timer singleshot call startSokconnection1 after 4 sec of tcpSocket was in connected state */
        
            tcpSocket1 = new QTcpSocket();
            in1.setDevice(tcpSocket1);
            in1.setVersion(QDataStream::Qt_5_15);
        
           tcpSocket1->connectToHost("19x.16x.51.xxx", xx051); //(ipHost, ipPort.toShort()); //ipPort.toShort());
            
           if (tcpSocket1->waitForConnected()){
                        qDebug("Connected -1-"); 
           }
            connect(tcpSocket1,&QAbstractSocket::disconnected,[]()->void{qDebug("Disconnected -1-");});
        
            connect(tcpSocket1, &QIODevice::readyRead, this, &MainWindow::readFortune1);
            connect(tcpSocket1, &QAbstractSocket::stateChanged, this, &MainWindow::sokState1);
            connect(tcpSocket1, &QAbstractSocket::errorOccurred, this, &MainWindow::displayError1);
        
        }
        
        
        

        when start tcpSocket1 tcpSocket go to Unconnected state ...... readFortune1 is never call actually .... readFortune is working when tcpSocket is connected ...

        bkt

        JonBJ jsulmJ 2 Replies Last reply
        0
        • gfxxG gfxx

          @jsulm quite simple .... server is done by qt example .... and client is these:

          /* on mainwindows.h */
          
          private: 
              QTcpSocket *tcpSocket;
              QDataStream in;
              QTcpSocket *tcpSocket1;
              QDataStream in1;
          
          /* on mainwindows.h   end  */
          
          /* on mainwindows  */
          tcpSocket = new QTcpSocket(this);
              in.setDevice(tcpSocket);
              in.setVersion(QDataStream::Qt_5_15);
          
             tcpSocket->connectToHost("19x.16x.50.xxx", xx050); //(ipHost, ipPort.toShort()); //ipPort.toShort());
              
             if (tcpSocket->waitForConnected()){
                          qDebug("Connected!");
             }
              connect(tcpSocket,&QAbstractSocket::disconnected,[]()->void{qDebug("Disconnected");});
          
              connect(tcpSocket, &QIODevice::readyRead, this, &MainWindow::readFortune);
              connect(tcpSocket, &QAbstractSocket::stateChanged, this, &MainWindow::sokState);
              connect(tcpSocket, &QAbstractSocket::errorOccurred, this, &MainWindow::displayError);
          
          /* end */ 
          
          
          void MainWindow::startSokconnection1(){   /* a timer singleshot call startSokconnection1 after 4 sec of tcpSocket was in connected state */
          
              tcpSocket1 = new QTcpSocket();
              in1.setDevice(tcpSocket1);
              in1.setVersion(QDataStream::Qt_5_15);
          
             tcpSocket1->connectToHost("19x.16x.51.xxx", xx051); //(ipHost, ipPort.toShort()); //ipPort.toShort());
              
             if (tcpSocket1->waitForConnected()){
                          qDebug("Connected -1-"); 
             }
              connect(tcpSocket1,&QAbstractSocket::disconnected,[]()->void{qDebug("Disconnected -1-");});
          
              connect(tcpSocket1, &QIODevice::readyRead, this, &MainWindow::readFortune1);
              connect(tcpSocket1, &QAbstractSocket::stateChanged, this, &MainWindow::sokState1);
              connect(tcpSocket1, &QAbstractSocket::errorOccurred, this, &MainWindow::displayError1);
          
          }
          
          
          

          when start tcpSocket1 tcpSocket go to Unconnected state ...... readFortune1 is never call actually .... readFortune is working when tcpSocket is connected ...

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @gfxx
          If you are under Linux where you have the telnet command, or if you have something similar under Windows, from separate shell/terminal instances set off these two commands:

          telnet 19x.16x.50.xxx xx050
          telnet 19x.16x.51.xxx xx051
          

          Don't worry that that you cannot "interact" with each one. Hopefully after the first one the telnet stays connected. Does it get disconnected after the second one? If so this is server behaviour, for whatever reason, nothing to do with Qt client. I note that in addition to the port numbers being different your IP addresses are also different....

          EDIT This was posted before @jsulm's reply below, which doubtless will turn out to be the actual issue....

          gfxxG 1 Reply Last reply
          1
          • gfxxG gfxx

            @jsulm quite simple .... server is done by qt example .... and client is these:

            /* on mainwindows.h */
            
            private: 
                QTcpSocket *tcpSocket;
                QDataStream in;
                QTcpSocket *tcpSocket1;
                QDataStream in1;
            
            /* on mainwindows.h   end  */
            
            /* on mainwindows  */
            tcpSocket = new QTcpSocket(this);
                in.setDevice(tcpSocket);
                in.setVersion(QDataStream::Qt_5_15);
            
               tcpSocket->connectToHost("19x.16x.50.xxx", xx050); //(ipHost, ipPort.toShort()); //ipPort.toShort());
                
               if (tcpSocket->waitForConnected()){
                            qDebug("Connected!");
               }
                connect(tcpSocket,&QAbstractSocket::disconnected,[]()->void{qDebug("Disconnected");});
            
                connect(tcpSocket, &QIODevice::readyRead, this, &MainWindow::readFortune);
                connect(tcpSocket, &QAbstractSocket::stateChanged, this, &MainWindow::sokState);
                connect(tcpSocket, &QAbstractSocket::errorOccurred, this, &MainWindow::displayError);
            
            /* end */ 
            
            
            void MainWindow::startSokconnection1(){   /* a timer singleshot call startSokconnection1 after 4 sec of tcpSocket was in connected state */
            
                tcpSocket1 = new QTcpSocket();
                in1.setDevice(tcpSocket1);
                in1.setVersion(QDataStream::Qt_5_15);
            
               tcpSocket1->connectToHost("19x.16x.51.xxx", xx051); //(ipHost, ipPort.toShort()); //ipPort.toShort());
                
               if (tcpSocket1->waitForConnected()){
                            qDebug("Connected -1-"); 
               }
                connect(tcpSocket1,&QAbstractSocket::disconnected,[]()->void{qDebug("Disconnected -1-");});
            
                connect(tcpSocket1, &QIODevice::readyRead, this, &MainWindow::readFortune1);
                connect(tcpSocket1, &QAbstractSocket::stateChanged, this, &MainWindow::sokState1);
                connect(tcpSocket1, &QAbstractSocket::errorOccurred, this, &MainWindow::displayError1);
            
            }
            
            
            

            when start tcpSocket1 tcpSocket go to Unconnected state ...... readFortune1 is never call actually .... readFortune is working when tcpSocket is connected ...

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @gfxx said in QTCP run 2 client on main thread, for 2 server, separate QAbstractSocket signal:

            in1.setDevice(tcpSocket);

            This is wrong

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            gfxxG 1 Reply Last reply
            0
            • jsulmJ jsulm

              @gfxx said in QTCP run 2 client on main thread, for 2 server, separate QAbstractSocket signal:

              in1.setDevice(tcpSocket);

              This is wrong

              gfxxG Offline
              gfxxG Offline
              gfxx
              wrote on last edited by
              #6

              @jsulm yes ... but ... nothing change after correction .... but readyRead1 is not used so maybe normal ...

              bkt

              1 Reply Last reply
              0
              • JonBJ JonB

                @gfxx
                If you are under Linux where you have the telnet command, or if you have something similar under Windows, from separate shell/terminal instances set off these two commands:

                telnet 19x.16x.50.xxx xx050
                telnet 19x.16x.51.xxx xx051
                

                Don't worry that that you cannot "interact" with each one. Hopefully after the first one the telnet stays connected. Does it get disconnected after the second one? If so this is server behaviour, for whatever reason, nothing to do with Qt client. I note that in addition to the port numbers being different your IP addresses are also different....

                EDIT This was posted before @jsulm's reply below, which doubtless will turn out to be the actual issue....

                gfxxG Offline
                gfxxG Offline
                gfxx
                wrote on last edited by gfxx
                #7

                @JonB telnet command work ....

                my situation about server .... and my app

                • SERVER 1 192.168.50.xxx -> TCP server (pc1)

                • SERVER 2 192.168.51.xxx -> TCP server 1 (pc2)

                • MYapp -> 2 client (pc3)

                • MYapp client1 on mainwindows previously report an error was:

                /* on mainwindows  */
                tcpSocket = new QTcpSocket(this);  // <-----
                    in.setDevice(tcpSocket);
                    in.setVersion(QDataStream::Qt_5_15);
                
                • MYapp client2 in in call whithout (this) ......

                I correct my previous code in previous post ..... but telnet work and there are not disconnection from server .... but I work in 2 different terminal not in same mainwindows app ....

                bkt

                jsulmJ 1 Reply Last reply
                0
                • gfxxG gfxx

                  @JonB telnet command work ....

                  my situation about server .... and my app

                  • SERVER 1 192.168.50.xxx -> TCP server (pc1)

                  • SERVER 2 192.168.51.xxx -> TCP server 1 (pc2)

                  • MYapp -> 2 client (pc3)

                  • MYapp client1 on mainwindows previously report an error was:

                  /* on mainwindows  */
                  tcpSocket = new QTcpSocket(this);  // <-----
                      in.setDevice(tcpSocket);
                      in.setVersion(QDataStream::Qt_5_15);
                  
                  • MYapp client2 in in call whithout (this) ......

                  I correct my previous code in previous post ..... but telnet work and there are not disconnection from server .... but I work in 2 different terminal not in same mainwindows app ....

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @gfxx There must be still something wrong with your code. Using two QTcpSocket instances to connect to two different servers at the same time works for sure. Can you post a minimal compilable application which shows this behaviour?

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  gfxxG 1 Reply Last reply
                  1
                  • jsulmJ jsulm

                    @gfxx There must be still something wrong with your code. Using two QTcpSocket instances to connect to two different servers at the same time works for sure. Can you post a minimal compilable application which shows this behaviour?

                    gfxxG Offline
                    gfxxG Offline
                    gfxx
                    wrote on last edited by
                    #9

                    @jsulm for first thanks a lot about these confirmation .... I not sure about ....

                    second because not sure I simplified my real question posted here:
                    https://forum.qt.io/topic/141619/qtcp-client-at-same-time-of-qtopcua-client /* I know I'm a bad person at all */

                    Actually I try start i real simply working compilable example .... think in one hour

                    bkt

                    gfxxG 1 Reply Last reply
                    0
                    • gfxxG gfxx

                      @jsulm for first thanks a lot about these confirmation .... I not sure about ....

                      second because not sure I simplified my real question posted here:
                      https://forum.qt.io/topic/141619/qtcp-client-at-same-time-of-qtopcua-client /* I know I'm a bad person at all */

                      Actually I try start i real simply working compilable example .... think in one hour

                      gfxxG Offline
                      gfxxG Offline
                      gfxx
                      wrote on last edited by
                      #10

                      ok .... i made a new app with soket only because problem is in that part ...... these not work at all ....

                      #ifndef MAINWINDOW_H
                      #define MAINWINDOW_H
                      
                      #include <QMainWindow>
                      #include <QtWidgets>
                      #include <QtNetwork/QNetworkAccessManager>
                      #include <QtNetwork>
                      #include <QKeyEvent>
                      #include <QInputDialog>
                      #include <QDate>
                      #include <QDateTime>
                      #include <QTcpSocket>
                      #include <QAbstractSocket>
                      #include <QDataStream>
                      
                      
                      QT_BEGIN_NAMESPACE
                      namespace Ui { class MainWindow; }
                      QT_END_NAMESPACE
                      
                      class MainWindow : public QMainWindow
                      {
                          Q_OBJECT
                      
                      public:
                          MainWindow(QWidget *parent = nullptr);
                          ~MainWindow();
                      
                      public slots:
                          void startSokconnection();
                      
                      private:
                          Ui::MainWindow *ui;
                          QTcpSocket *tcpSocket;
                          QDataStream in;
                      
                      };
                      
                      #include "mainwindow.h"
                      #include "ui_mainwindow.h"
                      
                      bool firstb = true;
                      
                      MainWindow::MainWindow(QWidget *parent)
                          : QMainWindow(parent)
                          , ui(new Ui::MainWindow)
                      {
                          ui->setupUi(this);
                      
                          tcpSocket = new QTcpSocket();
                          in.setDevice(tcpSocket);
                          in.setVersion(QDataStream::Qt_5_15);
                      
                          connect(ui->pb, SIGNAL(released()), this, SLOT(startSokconnection()));
                      
                          connect(tcpSocket,&QAbstractSocket::disconnected,[]()->void{qDebug("Disconnected");});
                          connect(tcpSocket,&QAbstractSocket::connected,[]()->void{qDebug("CONNECTED");});
                      
                      
                      }
                      
                      MainWindow::~MainWindow()
                      {
                          delete ui;
                      }
                      
                      void MainWindow::startSokconnection(){
                      
                          tcpSocket->abort();
                      
                          tcpSocket->connectToHost("192.168.50.xxx", 50xxx); //(ipHost, ipPort.toShort()); //ipPort.toShort());
                      
                          if (tcpSocket->waitForConnected()){
                              qDebug("Connected!");
                              firstb = false;
                          }
                      
                      
                      }
                      
                      

                      no error at all but nothing connect or disconnect messages .... and server seems not see client

                      What I loose?

                      bkt

                      gfxxG jsulmJ 2 Replies Last reply
                      0
                      • gfxxG gfxx

                        ok .... i made a new app with soket only because problem is in that part ...... these not work at all ....

                        #ifndef MAINWINDOW_H
                        #define MAINWINDOW_H
                        
                        #include <QMainWindow>
                        #include <QtWidgets>
                        #include <QtNetwork/QNetworkAccessManager>
                        #include <QtNetwork>
                        #include <QKeyEvent>
                        #include <QInputDialog>
                        #include <QDate>
                        #include <QDateTime>
                        #include <QTcpSocket>
                        #include <QAbstractSocket>
                        #include <QDataStream>
                        
                        
                        QT_BEGIN_NAMESPACE
                        namespace Ui { class MainWindow; }
                        QT_END_NAMESPACE
                        
                        class MainWindow : public QMainWindow
                        {
                            Q_OBJECT
                        
                        public:
                            MainWindow(QWidget *parent = nullptr);
                            ~MainWindow();
                        
                        public slots:
                            void startSokconnection();
                        
                        private:
                            Ui::MainWindow *ui;
                            QTcpSocket *tcpSocket;
                            QDataStream in;
                        
                        };
                        
                        #include "mainwindow.h"
                        #include "ui_mainwindow.h"
                        
                        bool firstb = true;
                        
                        MainWindow::MainWindow(QWidget *parent)
                            : QMainWindow(parent)
                            , ui(new Ui::MainWindow)
                        {
                            ui->setupUi(this);
                        
                            tcpSocket = new QTcpSocket();
                            in.setDevice(tcpSocket);
                            in.setVersion(QDataStream::Qt_5_15);
                        
                            connect(ui->pb, SIGNAL(released()), this, SLOT(startSokconnection()));
                        
                            connect(tcpSocket,&QAbstractSocket::disconnected,[]()->void{qDebug("Disconnected");});
                            connect(tcpSocket,&QAbstractSocket::connected,[]()->void{qDebug("CONNECTED");});
                        
                        
                        }
                        
                        MainWindow::~MainWindow()
                        {
                            delete ui;
                        }
                        
                        void MainWindow::startSokconnection(){
                        
                            tcpSocket->abort();
                        
                            tcpSocket->connectToHost("192.168.50.xxx", 50xxx); //(ipHost, ipPort.toShort()); //ipPort.toShort());
                        
                            if (tcpSocket->waitForConnected()){
                                qDebug("Connected!");
                                firstb = false;
                            }
                        
                        
                        }
                        
                        

                        no error at all but nothing connect or disconnect messages .... and server seems not see client

                        What I loose?

                        gfxxG Offline
                        gfxxG Offline
                        gfxx
                        wrote on last edited by
                        #11

                        @gfxx said in QTCP run 2 client on main thread, for 2 server, separate QAbstractSocket signal:

                        if (tcpSocket->waitForConnected()){
                            qDebug("Connected!");
                            firstb = false;
                        }
                        

                        because no msec time into waitForConnected macro .... abstractSocket switch immediately on unconnected state .... old part of my app containing a relais to call immediately reconnection id unconnected stare is reached .... so I see all that problem ....

                        Insert 3000ms and see al wheels .... thanks to all for support.

                        bkt

                        1 Reply Last reply
                        0
                        • gfxxG gfxx

                          ok .... i made a new app with soket only because problem is in that part ...... these not work at all ....

                          #ifndef MAINWINDOW_H
                          #define MAINWINDOW_H
                          
                          #include <QMainWindow>
                          #include <QtWidgets>
                          #include <QtNetwork/QNetworkAccessManager>
                          #include <QtNetwork>
                          #include <QKeyEvent>
                          #include <QInputDialog>
                          #include <QDate>
                          #include <QDateTime>
                          #include <QTcpSocket>
                          #include <QAbstractSocket>
                          #include <QDataStream>
                          
                          
                          QT_BEGIN_NAMESPACE
                          namespace Ui { class MainWindow; }
                          QT_END_NAMESPACE
                          
                          class MainWindow : public QMainWindow
                          {
                              Q_OBJECT
                          
                          public:
                              MainWindow(QWidget *parent = nullptr);
                              ~MainWindow();
                          
                          public slots:
                              void startSokconnection();
                          
                          private:
                              Ui::MainWindow *ui;
                              QTcpSocket *tcpSocket;
                              QDataStream in;
                          
                          };
                          
                          #include "mainwindow.h"
                          #include "ui_mainwindow.h"
                          
                          bool firstb = true;
                          
                          MainWindow::MainWindow(QWidget *parent)
                              : QMainWindow(parent)
                              , ui(new Ui::MainWindow)
                          {
                              ui->setupUi(this);
                          
                              tcpSocket = new QTcpSocket();
                              in.setDevice(tcpSocket);
                              in.setVersion(QDataStream::Qt_5_15);
                          
                              connect(ui->pb, SIGNAL(released()), this, SLOT(startSokconnection()));
                          
                              connect(tcpSocket,&QAbstractSocket::disconnected,[]()->void{qDebug("Disconnected");});
                              connect(tcpSocket,&QAbstractSocket::connected,[]()->void{qDebug("CONNECTED");});
                          
                          
                          }
                          
                          MainWindow::~MainWindow()
                          {
                              delete ui;
                          }
                          
                          void MainWindow::startSokconnection(){
                          
                              tcpSocket->abort();
                          
                              tcpSocket->connectToHost("192.168.50.xxx", 50xxx); //(ipHost, ipPort.toShort()); //ipPort.toShort());
                          
                              if (tcpSocket->waitForConnected()){
                                  qDebug("Connected!");
                                  firstb = false;
                              }
                          
                          
                          }
                          
                          

                          no error at all but nothing connect or disconnect messages .... and server seems not see client

                          What I loose?

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @gfxx said in QTCP run 2 client on main thread, for 2 server, separate QAbstractSocket signal:

                          connect(ui->pb, SIGNAL(released()), this, SLOT(startSokconnection()));

                          What is ui->pb? And did you make sure startSokconnection is called?

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          JonBJ 1 Reply Last reply
                          0
                          • jsulmJ jsulm

                            @gfxx said in QTCP run 2 client on main thread, for 2 server, separate QAbstractSocket signal:

                            connect(ui->pb, SIGNAL(released()), this, SLOT(startSokconnection()));

                            What is ui->pb? And did you make sure startSokconnection is called?

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #13

                            @jsulm
                            I assumed ui->pb is a QPushButton in Designer, with its released signal attached to startSokconnection.
                            I also assumed the OP has checked the button is pushed to release and the slot is called!

                            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