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 3.9k 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.
  • Christian EhrlicherC Christian Ehrlicher

    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();
    }
    
    
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on 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
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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

      KroMignonK 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

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

        JonBJ 1 Reply Last reply
        0
        • N NintyS

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #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
          • Christian EhrlicherC Christian Ehrlicher

            The waitForBytesWritten() is needed.

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on 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)

            Christian EhrlicherC J.HilkJ 2 Replies Last reply
            0
            • KroMignonK KroMignon

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

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 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
              • KroMignonK KroMignon

                @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.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on 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.

                KroMignonK JonBJ 2 Replies Last reply
                1
                • J.HilkJ J.Hilk

                  @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);
                          });
                  
                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on 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.HilkJ 1 Reply Last reply
                  0
                  • KroMignonK KroMignon

                    @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.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on 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.HilkJ J.Hilk

                      @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);
                              });
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #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

                      • Login

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