Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. Italian
  4. to send two message with server and to display into two client box in different time
Forum Updated to NodeBB v4.3 + New Features

to send two message with server and to display into two client box in different time

Scheduled Pinned Locked Moved Unsolved Italian
4 Posts 3 Posters 855 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.
  • MontanaroM Offline
    MontanaroM Offline
    Montanaro
    wrote on last edited by
    #1

    Hi I'm usign client/server fortune example.
    I want to send two messagge: the first when connection is established and the second after some minutis.
    And my server has to send messagge automatically.

    I have two message in client windows but in the same time. I want two messagge in different time.

    Can you suggest ?

    So I wrote the code:

    SERVER

    void Server::sendFortune()
    {
    
    
    //! [5]
        QByteArray block;
        QDataStream out(&block, QIODevice::WriteOnly);
        out.setVersion(QDataStream::Qt_5_10);
       fortunes << tr("CONNECTED\n  with  Seriale Number  1234567890  ");
        out << fortunes[QRandomGenerator::global()->bounded(fortunes.size())];
    
    
    
         QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
    
        clientConnection->write(block);
    
    
       sleep(100);
    
    
       fortunes1 << tr("WARNING\n from Seriale Number  1234567890  ");
    
       out << fortunes1[QRandomGenerator::global()->bounded(fortunes1.size())];
    
    
      clientConnection->write(block);
      
    }
    

    CLIENT

    :

    void Client::readFortune()
    {
    
        while(1)
        {
    
        in.startTransaction();
    
        QString nextFortune;
        in >> nextFortune;
    
    
        if (nextFortune != currentFortune) {
    
    
    
    
    
    
            if (count==1)
            {
             QMessageBox::information(this, tr("WARNING"), nextFortune);
     
    
            }
            count++;
        }
    
    
         statusLabel->setText(currentFortune);
    
      }
    
    JonBJ 1 Reply Last reply
    0
    • MontanaroM Montanaro

      Hi I'm usign client/server fortune example.
      I want to send two messagge: the first when connection is established and the second after some minutis.
      And my server has to send messagge automatically.

      I have two message in client windows but in the same time. I want two messagge in different time.

      Can you suggest ?

      So I wrote the code:

      SERVER

      void Server::sendFortune()
      {
      
      
      //! [5]
          QByteArray block;
          QDataStream out(&block, QIODevice::WriteOnly);
          out.setVersion(QDataStream::Qt_5_10);
         fortunes << tr("CONNECTED\n  with  Seriale Number  1234567890  ");
          out << fortunes[QRandomGenerator::global()->bounded(fortunes.size())];
      
      
      
           QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
      
          clientConnection->write(block);
      
      
         sleep(100);
      
      
         fortunes1 << tr("WARNING\n from Seriale Number  1234567890  ");
      
         out << fortunes1[QRandomGenerator::global()->bounded(fortunes1.size())];
      
      
        clientConnection->write(block);
        
      }
      

      CLIENT

      :

      void Client::readFortune()
      {
      
          while(1)
          {
      
          in.startTransaction();
      
          QString nextFortune;
          in >> nextFortune;
      
      
          if (nextFortune != currentFortune) {
      
      
      
      
      
      
              if (count==1)
              {
               QMessageBox::information(this, tr("WARNING"), nextFortune);
       
      
              }
              count++;
          }
      
      
           statusLabel->setText(currentFortune);
      
        }
      
      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @Montanaro

      and the second after some minutis

      Do not use sleep(). Use a QTimer.

      1 Reply Last reply
      1
      • MontanaroM Offline
        MontanaroM Offline
        Montanaro
        wrote on last edited by
        #3

        it doesnt work :(

        Socket thinks two messages are only one message :(

        jsulmJ 1 Reply Last reply
        0
        • MontanaroM Montanaro

          it doesnt work :(

          Socket thinks two messages are only one message :(

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

          @Montanaro Socket does not think anything, it just sends what you tell it to send.
          Please post your current code if it does not work the way you want.

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

          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