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. How do I know that the client has connected to the server?
Forum Updated to NodeBB v4.3 + New Features

How do I know that the client has connected to the server?

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 5 Posters 4.2k 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.
  • C Offline
    C Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 7 Feb 2021, 17:08 last edited by
    #6

    And with which program do you connect to this port? Are you sure it really connects to your address?
    And please use the new signal/slot syntax.

    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
    1
    • N Offline
      N Offline
      NintyS
      wrote on 7 Feb 2021, 17:10 last edited by
      #7

      No, I'm not sure but when I close the program then I have a message "connected", so I thinking I connect with my server.

      C 1 Reply Last reply 7 Feb 2021, 17:15
      0
      • N NintyS
        7 Feb 2021, 17:10

        No, I'm not sure but when I close the program then I have a message "connected", so I thinking I connect with my server.

        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 7 Feb 2021, 17:15 last edited by
        #8

        @NintyS said in How do I know that the client has connected to the server?:

        No, I'm not sure but when I close the program then I have a message "connected", so I thinking I connect with my server.

        Sorry but please be more precise. Why do you get a 'connected' when you close a program? This doesn't sounds very useful. What program is this? Please use a simple QTcpSocket as shown in the QTcpServer examples to check your program. Or try to connect with e.g. telnet to your listen port.

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

        N 1 Reply Last reply 8 Feb 2021, 07:45
        1
        • N Offline
          N Offline
          NintyS
          wrote on 7 Feb 2021, 17:34 last edited by
          #9

          Now this is a simple program what must connect to server and close. I will be try on my own hand, if I do it, I will write about it.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 7 Feb 2021, 17:45 last edited by
            #10

            This is working fine for me:

            #include <QtNetwork>
            
            int main(int argc, char* argv[])
            {
                QCoreApplication app(argc, argv);
                
                QTcpServer server;
                if (!server.listen(QHostAddress::Any, 50153))
                    return 1;
                QObject::connect(&server, &QTcpServer::newConnection,
                                 &server, [&server]() {
                    QTcpSocket *socket = server.nextPendingConnection();
                    qDebug() << "client connected from" << socket->peerAddress() << ":" << socket->peerPort();
                    socket->write("Test\n");
                    socket->flush();
                    socket->waitForBytesWritten(300);
                    socket->close();
                });
                
                QTcpSocket client;
                QObject::connect(&client, &QTcpSocket::readyRead,
                                 &client, [&client]() {
                    qDebug() << client.readAll();
                    QCoreApplication::quit();
                });
                client.connectToHost("localhost", 50153);
                
                return app.exec();
            }
            
            

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

            J 1 Reply Last reply 7 Feb 2021, 18:51
            2
            • N Offline
              N Offline
              NintyS
              wrote on 7 Feb 2021, 18:33 last edited by
              #11

              Thank you very much :D

              1 Reply Last reply
              0
              • C Christian Ehrlicher
                7 Feb 2021, 17:45

                This is working fine for me:

                #include <QtNetwork>
                
                int main(int argc, char* argv[])
                {
                    QCoreApplication app(argc, argv);
                    
                    QTcpServer server;
                    if (!server.listen(QHostAddress::Any, 50153))
                        return 1;
                    QObject::connect(&server, &QTcpServer::newConnection,
                                     &server, [&server]() {
                        QTcpSocket *socket = server.nextPendingConnection();
                        qDebug() << "client connected from" << socket->peerAddress() << ":" << socket->peerPort();
                        socket->write("Test\n");
                        socket->flush();
                        socket->waitForBytesWritten(300);
                        socket->close();
                    });
                    
                    QTcpSocket client;
                    QObject::connect(&client, &QTcpSocket::readyRead,
                                     &client, [&client]() {
                        qDebug() << client.readAll();
                        QCoreApplication::quit();
                    });
                    client.connectToHost("localhost", 50153);
                    
                    return app.exec();
                }
                
                
                J Online
                J Online
                JonB
                wrote on 7 Feb 2021, 18:51 last edited by
                #12

                @Christian-Ehrlicher said in How do I know that the client has connected to the server?:

                    socket->flush();
                    socket->waitForBytesWritten(300);
                

                Does one have to have these two lines prior to socket->close(), or are you just being "safe"?

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 7 Feb 2021, 18:56 last edited by
                  #13

                  The waitForBytesWritten() is needed.

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

                  K 1 Reply Last reply 8 Feb 2021, 10:24
                  1
                  • C Christian Ehrlicher
                    7 Feb 2021, 17:15

                    @NintyS said in How do I know that the client has connected to the server?:

                    No, I'm not sure but when I close the program then I have a message "connected", so I thinking I connect with my server.

                    Sorry but please be more precise. Why do you get a 'connected' when you close a program? This doesn't sounds very useful. What program is this? Please use a simple QTcpSocket as shown in the QTcpServer examples to check your program. Or try to connect with e.g. telnet to your listen port.

                    N Offline
                    N Offline
                    NintyS
                    wrote on 8 Feb 2021, 07:45 last edited by
                    #14

                    @Christian-Ehrlicher I know now why. I use a printf() to show the result. Now when I use qDebug I see my result after connection.

                    J 1 Reply Last reply 8 Feb 2021, 09:56
                    0
                    • N NintyS
                      8 Feb 2021, 07:45

                      @Christian-Ehrlicher I know now why. I use a printf() to show the result. Now when I use qDebug I see my result after connection.

                      J Online
                      J Online
                      JonB
                      wrote on 8 Feb 2021, 09:56 last edited by JonB 2 Aug 2021, 09:58
                      #15

                      @NintyS
                      printf() is liable to be at least line-buffered, maybe even fully buffered. You would have to follow it with fflush(stdout) to try to see the output immediately. fprintf(stderr, ...) would probably be better. In any case qDebug() is a better choice for debugging.

                      1 Reply Last reply
                      0
                      • C Christian Ehrlicher
                        7 Feb 2021, 18:56

                        The waitForBytesWritten() is needed.

                        K Offline
                        K Offline
                        KroMignon
                        wrote on 8 Feb 2021, 10:24 last edited by
                        #16

                        @Christian-Ehrlicher said in How do I know that the client has connected to the server?:

                        the waitForBytesWritten() is needed.

                        Why? My understanding was that waitForBytesWritten() is only required if not event queue is available... But without event queue, the connect() won't work.

                        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                        C J 2 Replies Last reply 8 Feb 2021, 10:27
                        0
                        • K KroMignon
                          8 Feb 2021, 10:24

                          @Christian-Ehrlicher said in How do I know that the client has connected to the server?:

                          the waitForBytesWritten() is needed.

                          Why? My understanding was that waitForBytesWritten() is only required if not event queue is available... But without event queue, the connect() won't work.

                          C Offline
                          C Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 8 Feb 2021, 10:27 last edited by
                          #17

                          @KroMignon Because of the close() directly afterwards. I'm pretty sure close() will call flush() before but it's not documented.

                          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
                          1
                          • K KroMignon
                            8 Feb 2021, 10:24

                            @Christian-Ehrlicher said in How do I know that the client has connected to the server?:

                            the waitForBytesWritten() is needed.

                            Why? My understanding was that waitForBytesWritten() is only required if not event queue is available... But without event queue, the connect() won't work.

                            J Offline
                            J Offline
                            J.Hilk
                            Moderators
                            wrote on 8 Feb 2021, 10:35 last edited by
                            #18

                            @KroMignon probably because of the immediate call to close on the socket

                            you could alternatively, potentially nest lambdas

                                   QTcpSocket *socket = server.nextPendingConnection();
                                        qDebug() << "client connected from" << socket->peerAddress() << ":" << socket->peerPort();
                                        QByteArray data{"Test\n"};
                                        QObject::connect(socket, &QTcpSocket::bytesWritten, socket, [socket, bytesToSend = data.size()](qint64 bytes)->void{
                                            static qint64 bytesSend{0};
                                            bytesSend += bytes;
                                            if(static_cast<qint64>(bytesToSend) == bytesSend)
                                                socket->close();
                                        });
                                        socket->write(data);
                                    });
                            

                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            K J 2 Replies Last reply 8 Feb 2021, 10:44
                            1
                            • J J.Hilk
                              8 Feb 2021, 10:35

                              @KroMignon probably because of the immediate call to close on the socket

                              you could alternatively, potentially nest lambdas

                                     QTcpSocket *socket = server.nextPendingConnection();
                                          qDebug() << "client connected from" << socket->peerAddress() << ":" << socket->peerPort();
                                          QByteArray data{"Test\n"};
                                          QObject::connect(socket, &QTcpSocket::bytesWritten, socket, [socket, bytesToSend = data.size()](qint64 bytes)->void{
                                              static qint64 bytesSend{0};
                                              bytesSend += bytes;
                                              if(static_cast<qint64>(bytesToSend) == bytesSend)
                                                  socket->close();
                                          });
                                          socket->write(data);
                                      });
                              
                              K Offline
                              K Offline
                              KroMignon
                              wrote on 8 Feb 2021, 10:44 last edited by
                              #19

                              @J-Hilk said in How do I know that the client has connected to the server?:

                              you could alternatively, potentially nest lambdas

                                     QTcpSocket *socket = server.nextPendingConnection();
                                          qDebug() << "client connected from" << socket->peerAddress() << ":" << socket->peerPort();
                                          QByteArray data{"Test\n"};
                                          QObject::connect(socket, &QTcpSocket::bytesWritten, socket, [socket, bytesToSend = data.size()](qint64 bytes)->void{
                                              static qint64 bytesSend{0};
                                              bytesSend += bytes;
                                              if(static_cast<qint64>(bytesToSend) == bytesSend)
                                                  socket->close();
                                          });
                                          socket->write(data);
                                      });
                              

                              Hmm, I would do it like this:

                              QTcpSocket *socket = server.nextPendingConnection();
                              qDebug() << "client connected from" << socket->peerAddress() << ":" << socket->peerPort();
                              QByteArray data{"Test\n"};
                              QObject::connect(socket, &QTcpSocket::bytesWritten, socket, [socket](qint64 bytes)->void{
                                  if(socket->bytesToWrite() == 0)
                                      socket->close();
                              });
                              socket->write(data);
                              

                              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                              J 1 Reply Last reply 8 Feb 2021, 10:49
                              0
                              • K KroMignon
                                8 Feb 2021, 10:44

                                @J-Hilk said in How do I know that the client has connected to the server?:

                                you could alternatively, potentially nest lambdas

                                       QTcpSocket *socket = server.nextPendingConnection();
                                            qDebug() << "client connected from" << socket->peerAddress() << ":" << socket->peerPort();
                                            QByteArray data{"Test\n"};
                                            QObject::connect(socket, &QTcpSocket::bytesWritten, socket, [socket, bytesToSend = data.size()](qint64 bytes)->void{
                                                static qint64 bytesSend{0};
                                                bytesSend += bytes;
                                                if(static_cast<qint64>(bytesToSend) == bytesSend)
                                                    socket->close();
                                            });
                                            socket->write(data);
                                        });
                                

                                Hmm, I would do it like this:

                                QTcpSocket *socket = server.nextPendingConnection();
                                qDebug() << "client connected from" << socket->peerAddress() << ":" << socket->peerPort();
                                QByteArray data{"Test\n"};
                                QObject::connect(socket, &QTcpSocket::bytesWritten, socket, [socket](qint64 bytes)->void{
                                    if(socket->bytesToWrite() == 0)
                                        socket->close();
                                });
                                socket->write(data);
                                
                                J Offline
                                J Offline
                                J.Hilk
                                Moderators
                                wrote on 8 Feb 2021, 10:49 last edited by
                                #20

                                @KroMignon potentially, I'm not familiar enough with the inner workings of QAbstractSocket to say for sure, that bytesToWrite is updated before bytesWritten is emitted 🤷‍♂️


                                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                Q: What's that?
                                A: It's blue light.
                                Q: What does it do?
                                A: It turns blue.

                                1 Reply Last reply
                                0
                                • J J.Hilk
                                  8 Feb 2021, 10:35

                                  @KroMignon probably because of the immediate call to close on the socket

                                  you could alternatively, potentially nest lambdas

                                         QTcpSocket *socket = server.nextPendingConnection();
                                              qDebug() << "client connected from" << socket->peerAddress() << ":" << socket->peerPort();
                                              QByteArray data{"Test\n"};
                                              QObject::connect(socket, &QTcpSocket::bytesWritten, socket, [socket, bytesToSend = data.size()](qint64 bytes)->void{
                                                  static qint64 bytesSend{0};
                                                  bytesSend += bytes;
                                                  if(static_cast<qint64>(bytesToSend) == bytesSend)
                                                      socket->close();
                                              });
                                              socket->write(data);
                                          });
                                  
                                  J Online
                                  J Online
                                  JonB
                                  wrote on 8 Feb 2021, 11:24 last edited by JonB 2 Aug 2021, 11:25
                                  #21

                                  @J-Hilk said in How do I know that the client has connected to the server?:

                                  static qint64 bytesSend{0};

                                  You might like to come join us in my question about this over at https://forum.qt.io/topic/123582/static-in-lambda ? :)

                                  1 Reply Last reply
                                  0

                                  15/21

                                  8 Feb 2021, 09:56

                                  • Login

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