Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. How to send data from client to server QT TCP/IP
Forum Updated to NodeBB v4.3 + New Features

How to send data from client to server QT TCP/IP

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
5 Posts 2 Posters 1.6k 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.
  • E Offline
    E Offline
    ELIF
    wrote on 10 May 2022, 06:36 last edited by ELIF 5 Oct 2022, 07:22
    #1

    I have two QT Widget project,Client and HelloWorldServer.My aim
    1.Start server
    2.Constitute a message part in client side then send this message client to server ui text element.

    In client mainwindow.cpp I have

    void MainWindow::on_pushButton_clicked()//CONNECT TO SERVER(host)
    {
       m_clientSocket->connectToHost(ui->textEditIpAdrress->toPlainText(),quint16(ui->textEditPortAdrres->toPlainText().toInt()));
       
     if (m_clientSocket->waitForConnected(1000))
        qDebug("Connected!");
        connect(m_clientSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
    }
    
    void MainWindow::displayError(QAbstractSocket::SocketError socketError){
    
    switch(socketError){
    case QAbstractSocket::RemoteHostClosedError:
        QMessageBox::information(this,tr("connection was refused"),tr("RemoteHostClosedError"));
        break;
    case QAbstractSocket::HostNotFoundError:
        QMessageBox::information(this,tr("connection was refused"),tr("HostNotFoundError"));
        break;
    case QAbstractSocket::ConnectionRefusedError:
        QMessageBox::information(this,tr("Fortune Client"),tr("ConnectionRefusedError"));
        break;and
    
    default:
        QMessageBox::information(this,tr("Fortune Client"), tr("the following error occured %1.").arg(m_clientSocket->errorString()));
    
      }
      }
    
    void MainWindow::on_pushButton_2_clicked()//send data via socket to host
    {
        QString message=ui->textEditMessageAdres->toPlainText().trimmed();
        if(!message.isEmpty()){
            m_clientSocket->write(QString(message + "\n").toUtf8());
            qDebug() <<message;
        }
      ui->textEditMessageAdres->clear();
      ui->textEditMessageAdres->setFocus();
    }
    

    In server helloworldserver.cpp I have

    HelloworldServer::HelloworldServer(Server *pHelloServer,QObject *parent):QTcpServer(parent)
    {
          m_pHelloWindow = pHelloServer;
    }
    
    
    void HelloworldServer::incomingConnection(int socketfd){
     QTcpSocket *client=new QTcpSocket(this);
     client->setSocketDescriptor(socketfd);
     clients.insert(client);
     m_pHelloWindow->addMessage("New client from: " + client- 
     >peerAddress().toString());
     connect(client,SIGNAL(readyRead()),this,SLOT(readyRead()));
     connect(client,SIGNAL(disconnected()),this,SLOT(disconnected()));
    }
    
    void HelloworldServer::readyRead(){ //I want to get the message from client 
        QTcpSocket *client= (QTcpSocket*) sender();
        
        while(client->canReadLine()){
         
            QString line = QString::fromUtf8(client->readLine()).trimmed();
            m_pHelloWindow->addMessage(line);
            qDebug() << "  " << line;
            qDebug() << client->readAll();
        }
    }
    void HelloworldServer::disconnected(){
        QTcpSocket  *client= (QTcpSocket*) sender();
        qDebug()<<"Client disconnected :"<< client->peerAddress().toString();
        clients.remove(client);
    }
    

    When I click the Connect there is no connect message on console.
    220861bd-1711-4a11-af19-859a20b651f7-image.png

    When I click send button I want to see it on server side (on status message) but there is no message on server side.
    1ec86dfe-11be-46c7-bb95-5f04f41cc199-image.png

    How to solve this issue?Any helps will be appreciated! Thanks.

    J 1 Reply Last reply 10 May 2022, 07:24
    0
    • E ELIF
      10 May 2022, 06:36

      I have two QT Widget project,Client and HelloWorldServer.My aim
      1.Start server
      2.Constitute a message part in client side then send this message client to server ui text element.

      In client mainwindow.cpp I have

      void MainWindow::on_pushButton_clicked()//CONNECT TO SERVER(host)
      {
         m_clientSocket->connectToHost(ui->textEditIpAdrress->toPlainText(),quint16(ui->textEditPortAdrres->toPlainText().toInt()));
         
       if (m_clientSocket->waitForConnected(1000))
          qDebug("Connected!");
          connect(m_clientSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
      }
      
      void MainWindow::displayError(QAbstractSocket::SocketError socketError){
      
      switch(socketError){
      case QAbstractSocket::RemoteHostClosedError:
          QMessageBox::information(this,tr("connection was refused"),tr("RemoteHostClosedError"));
          break;
      case QAbstractSocket::HostNotFoundError:
          QMessageBox::information(this,tr("connection was refused"),tr("HostNotFoundError"));
          break;
      case QAbstractSocket::ConnectionRefusedError:
          QMessageBox::information(this,tr("Fortune Client"),tr("ConnectionRefusedError"));
          break;and
      
      default:
          QMessageBox::information(this,tr("Fortune Client"), tr("the following error occured %1.").arg(m_clientSocket->errorString()));
      
        }
        }
      
      void MainWindow::on_pushButton_2_clicked()//send data via socket to host
      {
          QString message=ui->textEditMessageAdres->toPlainText().trimmed();
          if(!message.isEmpty()){
              m_clientSocket->write(QString(message + "\n").toUtf8());
              qDebug() <<message;
          }
        ui->textEditMessageAdres->clear();
        ui->textEditMessageAdres->setFocus();
      }
      

      In server helloworldserver.cpp I have

      HelloworldServer::HelloworldServer(Server *pHelloServer,QObject *parent):QTcpServer(parent)
      {
            m_pHelloWindow = pHelloServer;
      }
      
      
      void HelloworldServer::incomingConnection(int socketfd){
       QTcpSocket *client=new QTcpSocket(this);
       client->setSocketDescriptor(socketfd);
       clients.insert(client);
       m_pHelloWindow->addMessage("New client from: " + client- 
       >peerAddress().toString());
       connect(client,SIGNAL(readyRead()),this,SLOT(readyRead()));
       connect(client,SIGNAL(disconnected()),this,SLOT(disconnected()));
      }
      
      void HelloworldServer::readyRead(){ //I want to get the message from client 
          QTcpSocket *client= (QTcpSocket*) sender();
          
          while(client->canReadLine()){
           
              QString line = QString::fromUtf8(client->readLine()).trimmed();
              m_pHelloWindow->addMessage(line);
              qDebug() << "  " << line;
              qDebug() << client->readAll();
          }
      }
      void HelloworldServer::disconnected(){
          QTcpSocket  *client= (QTcpSocket*) sender();
          qDebug()<<"Client disconnected :"<< client->peerAddress().toString();
          clients.remove(client);
      }
      

      When I click the Connect there is no connect message on console.
      220861bd-1711-4a11-af19-859a20b651f7-image.png

      When I click send button I want to see it on server side (on status message) but there is no message on server side.
      1ec86dfe-11be-46c7-bb95-5f04f41cc199-image.png

      How to solve this issue?Any helps will be appreciated! Thanks.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 10 May 2022, 07:24 last edited by
      #2

      @ELIF From the code you posted it is not clear whether your server is listening for incoming connections at all.
      Is your HelloworldServer a subclass of QTcpServer?

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

      E 1 Reply Last reply 10 May 2022, 07:32
      0
      • E Offline
        E Offline
        ELIF
        wrote on 10 May 2022, 07:29 last edited by
        #3

        @jsulm said in How to send data from client to server QT TCP/IP:

        HelloworldServer a subclass of QTcpServer?

        yes HelloworldServer is a subclass of QTcpServer? We can see it below

        HelloworldServer::HelloworldServer(Server *pHelloServer,QObject *parent): QTcpServer(parent)

        J 1 Reply Last reply 10 May 2022, 07:33
        0
        • J jsulm
          10 May 2022, 07:24

          @ELIF From the code you posted it is not clear whether your server is listening for incoming connections at all.
          Is your HelloworldServer a subclass of QTcpServer?

          E Offline
          E Offline
          ELIF
          wrote on 10 May 2022, 07:32 last edited by ELIF 5 Oct 2022, 07:32
          #4

          @jsulm said in How to send data from client to server QT TCP/IP:

          your server is listening for incoming connections at all.

          b82a9715-3e47-480e-8746-3703a294016a-image.png

          1 Reply Last reply
          0
          • E ELIF
            10 May 2022, 07:29

            @jsulm said in How to send data from client to server QT TCP/IP:

            HelloworldServer a subclass of QTcpServer?

            yes HelloworldServer is a subclass of QTcpServer? We can see it below

            HelloworldServer::HelloworldServer(Server *pHelloServer,QObject *parent): QTcpServer(parent)

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 10 May 2022, 07:33 last edited by
            #5

            @ELIF Then the signature here is wrong: void incomingConnection(int socketfd)
            Correct one is void incomingConnection(qintptr socketDescriptor)
            int != qintptr.
            You should add "override" keyword after incomingConnection declaration, so the compiler can check whether you're really overriding this method.

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

            1 Reply Last reply
            2

            1/5

            10 May 2022, 06:36

            • Login

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