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 server ... possible run more than one?
Forum Updated to NodeBB v4.3 + New Features

QTCP server ... possible run more than one?

Scheduled Pinned Locked Moved Unsolved General and Desktop
24 Posts 4 Posters 3.3k 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.
  • gfxxG Offline
    gfxxG Offline
    gfxx
    wrote on last edited by gfxx
    #1

    Problem become because use persisten connection from 4/5 client so instead to use multiple connection with qmapper ... i think to run more than one server with different port number ... so can be asyncronous and think not need to handle connection because connection is one to one .... but is possible?? It work .... data few there are not connection problem .... my idea was start server in these manner:

    server1 = new QTcpServer(this);
    connect(server1, SIGNAL(newConnection()), this, SLOT(newCon1()));
        if(!server->listen(QHostAddress("19x.16x.51.xxx"), xx051)
            qDebug() << "Server1 could not start";
    
    server2 = new QTcpServer(this);
    connect(server2, SIGNAL(newConnection()), this, SLOT(newCon2()));
        if(!server->listen(QHostAddress("19x.16x.51.xxx"), xx052)
            qDebug() << "Server 2could not start";
    
    server3 = new QTcpServer(this);
    connect(server3, SIGNAL(newConnection()), this, SLOT(newCon3()));
        if(!server->listen(QHostAddress("19x.16x.51.xxx"), xx053)
            qDebug() << "Server3 could not start";
    
    server4 = new QTcpServer(this);
    connect(server4, SIGNAL(newConnection()), this, SLOT(newCon4()));
        if(!server->listen(QHostAddress("19x.16x.51.xxx"), xx054)
            qDebug() << "Server4 could not start";
    
    

    there are a so bad ideas?

    bkt

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Why should this be needed? Handle each incoming connection in an own worker object and you're fine.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      gfxxG 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        Why should this be needed? Handle each incoming connection in an own worker object and you're fine.

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

        @Christian-Ehrlicher so you suggest to convert all stuff for read single string, read file and write single string in qthread? So 3/4 or 10 qthread?

        because client stay connected, and every 10sec send a string ... that string is a request of data or a command ... client can send 5/6 different command and based on command server can reply or send your own request .... i've not so mutch experiences with working thread (so my doubt) but seems a lot of things to do in working thread ....

        bkt

        Christian EhrlicherC 1 Reply Last reply
        0
        • gfxxG gfxx

          @Christian-Ehrlicher so you suggest to convert all stuff for read single string, read file and write single string in qthread? So 3/4 or 10 qthread?

          because client stay connected, and every 10sec send a string ... that string is a request of data or a command ... client can send 5/6 different command and based on command server can reply or send your own request .... i've not so mutch experiences with working thread (so my doubt) but seems a lot of things to do in working thread ....

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @gfxx said in QTCP server ... possible run more than one?:

          in qthread? So 3/4 or 10 qthread?

          I never ever said anything about a thread here. I told you to apply your c++ knowledge about encapsulation and handle every connection in an separate object of the same class...

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          gfxxG 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @gfxx said in QTCP server ... possible run more than one?:

            in qthread? So 3/4 or 10 qthread?

            I never ever said anything about a thread here. I told you to apply your c++ knowledge about encapsulation and handle every connection in an separate object of the same class...

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

            @Christian-Ehrlicher ok thanks ... my misunderstand ....
            but starting poit can be

            client = server->nextPendingConnection();
            chooseClient = client->peerAddress().toString();
            QString client1 = "19x.16x.51.xx1";
            QString client2 = "19x.16x.51.xx2";
            QString client3 = "19x.16x.51.xx3";
            QString client4 = "19x.16x.51.xx4";
            if(chooseClient == client1){ some stuff1... /* how to assign actual connection to right socket1 with these ip?? */ }
            else if(chooseClient == client2) { some stuff2../* how to assign actual connection to right socket2 with these ip?? */ } etc etc
            

            after newconnection .....
            I don't understand how I can distinguish one connection from another .... so let's assume that a client disconnects and then reconnects again .... the same call to newconnection will have to tell which client it is and it seems to me that in order to have a unique name getperradress is the only street ....

            bkt

            jsulmJ Christian EhrlicherC 2 Replies Last reply
            0
            • gfxxG gfxx

              @Christian-Ehrlicher ok thanks ... my misunderstand ....
              but starting poit can be

              client = server->nextPendingConnection();
              chooseClient = client->peerAddress().toString();
              QString client1 = "19x.16x.51.xx1";
              QString client2 = "19x.16x.51.xx2";
              QString client3 = "19x.16x.51.xx3";
              QString client4 = "19x.16x.51.xx4";
              if(chooseClient == client1){ some stuff1... /* how to assign actual connection to right socket1 with these ip?? */ }
              else if(chooseClient == client2) { some stuff2../* how to assign actual connection to right socket2 with these ip?? */ } etc etc
              

              after newconnection .....
              I don't understand how I can distinguish one connection from another .... so let's assume that a client disconnects and then reconnects again .... the same call to newconnection will have to tell which client it is and it seems to me that in order to have a unique name getperradress is the only street ....

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

              @gfxx said in QTCP server ... possible run more than one?:

              I don't understand how I can distinguish one connection from another

              For each connection you get a QTcpSocket. Don't know what the problem is. Can you explain why you need to know whether exact same client connects again? What is your use case?

              "it seems to me that in order to have a unique name getperradress is the only street" - you can have more than one connection from same IP. A connection is defined by IP + port.

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

              gfxxG 2 Replies Last reply
              0
              • gfxxG gfxx

                @Christian-Ehrlicher ok thanks ... my misunderstand ....
                but starting poit can be

                client = server->nextPendingConnection();
                chooseClient = client->peerAddress().toString();
                QString client1 = "19x.16x.51.xx1";
                QString client2 = "19x.16x.51.xx2";
                QString client3 = "19x.16x.51.xx3";
                QString client4 = "19x.16x.51.xx4";
                if(chooseClient == client1){ some stuff1... /* how to assign actual connection to right socket1 with these ip?? */ }
                else if(chooseClient == client2) { some stuff2../* how to assign actual connection to right socket2 with these ip?? */ } etc etc
                

                after newconnection .....
                I don't understand how I can distinguish one connection from another .... so let's assume that a client disconnects and then reconnects again .... the same call to newconnection will have to tell which client it is and it seems to me that in order to have a unique name getperradress is the only street ....

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @gfxx said in QTCP server ... possible run more than one?:

                after newconnection .....

                No, this is crap.

                Again: create a worker object for each connection and do your handling in there.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                gfxxG 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @gfxx said in QTCP server ... possible run more than one?:

                  I don't understand how I can distinguish one connection from another

                  For each connection you get a QTcpSocket. Don't know what the problem is. Can you explain why you need to know whether exact same client connects again? What is your use case?

                  "it seems to me that in order to have a unique name getperradress is the only street" - you can have more than one connection from same IP. A connection is defined by IP + port.

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

                  @jsulm because I share data per client request and data need to go to that client .... so for know what type of data need to send, need to know if that client is connect or not .... plus when receive data need to know witch client send that data .... because packet not contain client id so not possible filter it ....

                  Sorry but for me socket is a quite new affairs ....

                  bkt

                  jsulmJ 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @gfxx said in QTCP server ... possible run more than one?:

                    after newconnection .....

                    No, this is crap.

                    Again: create a worker object for each connection and do your handling in there.

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

                    @Christian-Ehrlicher .... you are in right

                    bkt

                    1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @gfxx said in QTCP server ... possible run more than one?:

                      I don't understand how I can distinguish one connection from another

                      For each connection you get a QTcpSocket. Don't know what the problem is. Can you explain why you need to know whether exact same client connects again? What is your use case?

                      "it seems to me that in order to have a unique name getperradress is the only street" - you can have more than one connection from same IP. A connection is defined by IP + port.

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

                      @jsulm for me is almost all new ..... so how can i figure out which client machine connected? .... auto reply: if build a sokClass and new istance of class every newconnection .... no need to know that .... so

                      void MainWindow::initServer(){
                      
                          server = new QTcpServer(this);
                          client = new Sokclient();
                          client1 = new Sokclient();
                          client2 = new Sokclient();
                      
                          if (!server->listen(QHostAddress("192.1xx.xx.xxx"), xxx)){
                              QString ipi = "no connection";
                              //return;
                          }
                          else{
                      /****  these is all wrong .... how can connect nexpendingconnection to new SockClient class object client, client1 and client2?? *****/
                              //connect(server, &QTcpServer::newConnection, this, [=](){client = server->nextPendingConnection(); new_con(); qDebug() << " connect ";});
                              //connect(client, &QTcpSocket::readyRead, this, &MainWindow::readSocket);
                              //connect(client, &QTcpSocket::disconnected,this, &MainWindow::discardSocket);
                              //connect(ui->pbtn_F_send, SIGNAL(released()), this, SLOT(sendOrder()));  
                      
                      

                      then need connect mainwindows signal to SokClient class slot ....

                      bkt

                      1 Reply Last reply
                      0
                      • gfxxG gfxx

                        @jsulm because I share data per client request and data need to go to that client .... so for know what type of data need to send, need to know if that client is connect or not .... plus when receive data need to know witch client send that data .... because packet not contain client id so not possible filter it ....

                        Sorry but for me socket is a quite new affairs ....

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

                        @gfxx said in QTCP server ... possible run more than one?:

                        when receive data need to know witch client send that data

                        You know from which client you got data because for each client you have a dedicated QTcpSocket instance (as I already wrote above). So, you simply write to the same socket from which you got data...

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

                        gfxxG 1 Reply Last reply
                        1
                        • jsulmJ jsulm

                          @gfxx said in QTCP server ... possible run more than one?:

                          when receive data need to know witch client send that data

                          You know from which client you got data because for each client you have a dedicated QTcpSocket instance (as I already wrote above). So, you simply write to the same socket from which you got data...

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

                          @jsulm sorry non understand ...

                          connect(server, &QTcpServer::newConnection, this, [=](){client = server->nextPendingConnection()
                          

                          call "client" on mainwindows ...... next connection can't be "client" .... at least need to be "client2" ..... how can i pass server->nextpendingconnection once to client2, then to client3 etc etc??

                          bkt

                          jsulmJ 1 Reply Last reply
                          0
                          • Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            Again: create a worker object which handles each connection for you, store the worker objects in a container if you really need to access them from anywhere else. Learn basic c++ principles.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            1 Reply Last reply
                            0
                            • gfxxG gfxx

                              @jsulm sorry non understand ...

                              connect(server, &QTcpServer::newConnection, this, [=](){client = server->nextPendingConnection()
                              

                              call "client" on mainwindows ...... next connection can't be "client" .... at least need to be "client2" ..... how can i pass server->nextpendingconnection once to client2, then to client3 etc etc??

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

                              @gfxx said in QTCP server ... possible run more than one?:

                              next connection can't be "client" .... at least need to be "client2"

                              Nothing stops you from storing all the QTcpSocket instances in a container (like QVector for example), like @Christian-Ehrlicher suggested already...

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

                              gfxxG 1 Reply Last reply
                              0
                              • jsulmJ jsulm

                                @gfxx said in QTCP server ... possible run more than one?:

                                next connection can't be "client" .... at least need to be "client2"

                                Nothing stops you from storing all the QTcpSocket instances in a container (like QVector for example), like @Christian-Ehrlicher suggested already...

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

                                @jsulm thanks .... but these means every time need one istance need to choose the right from qvector ... or qlist or other container .... so problem is almost the same .... why start soket1, soket2, soket3 etc etc and never stop it is crap like Christian talk? really not understand ....

                                bkt

                                Christian EhrlicherC jsulmJ 2 Replies Last reply
                                0
                                • gfxxG gfxx

                                  @jsulm thanks .... but these means every time need one istance need to choose the right from qvector ... or qlist or other container .... so problem is almost the same .... why start soket1, soket2, soket3 etc etc and never stop it is crap like Christian talk? really not understand ....

                                  Christian EhrlicherC Offline
                                  Christian EhrlicherC Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @gfxx said in QTCP server ... possible run more than one?:

                                  but these means every time need one istance need to choose the right from qvector

                                  Why should you need one of these instances?

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  1 Reply Last reply
                                  0
                                  • gfxxG gfxx

                                    @jsulm thanks .... but these means every time need one istance need to choose the right from qvector ... or qlist or other container .... so problem is almost the same .... why start soket1, soket2, soket3 etc etc and never stop it is crap like Christian talk? really not understand ....

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

                                    @gfxx said in QTCP server ... possible run more than one?:

                                    really not understand

                                    And I don't understand the problem. Can you explain when you need these instances?

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

                                    gfxxG 1 Reply Last reply
                                    0
                                    • jsulmJ jsulm

                                      @gfxx said in QTCP server ... possible run more than one?:

                                      really not understand

                                      And I don't understand the problem. Can you explain when you need these instances?

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

                                      @jsulm because data exchange is quite continuos and whitout any sort of regularity in format and type .... sometime file, sometime messages/string, sometime array/data structures .... if socket was interrupt need to have an advices and if need need to restart it 24/24 hour, 7/7 days ....

                                      bkt

                                      Christian EhrlicherC 1 Reply Last reply
                                      0
                                      • gfxxG gfxx

                                        @jsulm because data exchange is quite continuos and whitout any sort of regularity in format and type .... sometime file, sometime messages/string, sometime array/data structures .... if socket was interrupt need to have an advices and if need need to restart it 24/24 hour, 7/7 days ....

                                        Christian EhrlicherC Offline
                                        Christian EhrlicherC Offline
                                        Christian Ehrlicher
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        @gfxx said in QTCP server ... possible run more than one?:

                                        ecause data exchange is quite continuos and whitout any sort of regularity in format and type .... sometime file, sometime messages/string, sometime array/data structures .... if socket was interrupt need to have an advices and if need need to restart it 24/24 hour, 7/7 days ....

                                        This does not answer any of our questions and does not even give a hint on what exact problems you have when you do a proper c++ encapsulation of the sockets in a worker thread... but maybe my c++ knowledge is to small.

                                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                        Visit the Qt Academy at https://academy.qt.io/catalog

                                        gfxxG 1 Reply Last reply
                                        0
                                        • Christian EhrlicherC Christian Ehrlicher

                                          @gfxx said in QTCP server ... possible run more than one?:

                                          ecause data exchange is quite continuos and whitout any sort of regularity in format and type .... sometime file, sometime messages/string, sometime array/data structures .... if socket was interrupt need to have an advices and if need need to restart it 24/24 hour, 7/7 days ....

                                          This does not answer any of our questions and does not even give a hint on what exact problems you have when you do a proper c++ encapsulation of the sockets in a worker thread... but maybe my c++ knowledge is to small.

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

                                          @Christian-Ehrlicher said in QTCP server ... possible run more than one?:

                                          ... but maybe my c++ knowledge is to small.

                                          these is for make me laugh! thanks

                                          so i try with worker thread and mySok class ... qabstractsocket::state:unconnected for kill worker ...

                                          bkt

                                          Christian EhrlicherC 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