Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. QT an QTcpSocket
Qt 6.11 is out! See what's new in the release blog

QT an QTcpSocket

Scheduled Pinned Locked Moved Installation and Deployment
2 Posts 2 Posters 1.8k 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.
  • B Offline
    B Offline
    boroowa
    wrote on last edited by
    #1

    Hi!
    I have very important question. I have used QT to programming very simple TCP server - I want that client gets two variables: x and y and send it to server ant it should sum them and return this sum to client. My code is very similar to Threated Fortune Server & Fortune Client. However, I change method "run" in fortunethread.h to

    @
    ...

    void FortuneThread::run()
    {
    //emit msgSignal(QString::number(socketDescriptor));
    readFromSocket();
    }

    void FortuneThread::readFromSocket()
    {
    //QString numbers;

        QTcpSocket tcpSocket;
    
        if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
         emit error(tcpSocket.error());
         return;
     }
    
    
         if(tcpSocket.isReadable()) //(1)
         {
                QDataStream in(&tcpSocket);
                in.setVersion(QDataStream::Qt_4_0);
    
                QString numbers;
    
                QByteArray arr;
    
                in >> arr;
    
                QString k (arr.data());
    

    --
    //this signal opened QMessageBox with message in parameter
    emit msgSignal("Deskryptor: " + QString::number(socketDescriptor) + "\nDane: " + k);
    }
    else
    {
    emit msgSignal("asdfghjklasdfghjkl");
    }

        tcpSocket.disconnectFromHost();
        tcpSocket.waitForDisconnected();
    

    }

    ...

    @

    and cliend should send data to server (after press getFortuneButton):

    @
    void Client::writeToSocket(){

                tcpSocket->connectToHost(hostLineEdit->text(),
                                                                 portLineEdit->text().toInt());        
    
                QString num = (x + " " + y);
                QByteArray tab = num.toAscii();
    
                // QMessageBox b;
                // b.setText(x + " + " + y);
                // b.exec();
    
                tcpSocket->write(tab);
                tcpSocket->disconnectFromHost();
                tcpSocket->close();
    

    }

    @

    Why it isn't work?? Thanks for help :D

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VanDerSam
      wrote on last edited by
      #2

      QTcpSocket has asynchronious model of work. Send data after connection will be established.

      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