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. Client To server message sending
Forum Updated to NodeBB v4.3 + New Features

Client To server message sending

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 3 Posters 1.0k 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, 15:03 last edited by
    #1

    I have two application Client and HelloWorldServer
    40c9cbc9-b7c7-4165-bd16-3e2bdf55116b-image.png

    In client part
    1cfa5bf0-7764-4ac9-bc8a-e72d18046de0-image.png

    When I run these two application I want to see client message on Server status message .What is the problem and how can I solve it?
    100cdeb5-e6d8-43ca-8c1b-ace6e378579c-image.png

    J 1 Reply Last reply 10 May 2022, 15:22
    0
    • E ELIF
      10 May 2022, 15:03

      I have two application Client and HelloWorldServer
      40c9cbc9-b7c7-4165-bd16-3e2bdf55116b-image.png

      In client part
      1cfa5bf0-7764-4ac9-bc8a-e72d18046de0-image.png

      When I run these two application I want to see client message on Server status message .What is the problem and how can I solve it?
      100cdeb5-e6d8-43ca-8c1b-ace6e378579c-image.png

      J Offline
      J Offline
      JonB
      wrote on 10 May 2022, 15:22 last edited by
      #2

      @ELIF
      Please post code as code (and in Code tags), not as screenshots, so that people can copy & paste etc.

      • You do not say whether the client outputs Connected!. What happens in your code if waitForConnected(1000) fails? You still proceed...
      • You start a transaction on the socket, no sign of ending the transaction.
      • Does your code allow you to press pushButton_2 more than once? Is that what you are doing? Is the error message because it is re-using m_clientSocket and trying to connect again when it is already connected?

      At your server side you have qDebug() << client->readAll(). That will read and discard anything available after the first line is read. Is that what you intend?

      Otherwise put in qDebug() statements and actually show what they report if things are inexplicably not working.

      1 Reply Last reply
      1
      • V Offline
        V Offline
        VRonin
        wrote on 10 May 2022, 15:35 last edited by
        #3

        You probably want to take a look at this tutorial: https://wiki.qt.io/WIP-How_to_create_a_simple_chat_application

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        1
        • E Offline
          E Offline
          ELIF
          wrote on 12 May 2022, 11:32 last edited by
          #4

          I get the value of IP adress in below code on textEditMesaage (ui)
          m_pHelloWindow->addMessage("New client from: " + client->peerAddress().toString()); //ITS OK
          BUT in below code

          readyRead() function is not working. I want to solve it I really struggle, so sad:(
          Here is helloworldserver.cpp

          #include "helloworldserver.h"
          #include "server.h"
          #include "server.cpp"
          #include "ui_server.h"
          #include<list>
          #include<string.h>
          #include <QList>
          #include "server.h"//MAIN WINDOW
          #include "ui_server.h"
          
          HelloworldServer::HelloworldServer(Server *pHelloServer,QObject *parent):QTcpServer(parent){
          
              m_pHelloWindow = pHelloServer;
          }
          
          void HelloworldServer::readyRead(){
          
              QTcpSocket * client = (QTcpSocket *)sender();
              client->open(QIODevice::ReadWrite);
              QByteArray data=client->readAll();
              client->waitForReadyRead(100);
              m_pHelloWindow->addMessage(" : " + client->write(data));
          
          }
          void HelloworldServer::incomingConnection(qintptr handle){
              QTcpSocket *client=new QTcpSocket(this);
              client->setSocketDescriptor(handle);
              clients.insert(client);
              m_pHelloWindow->addMessage("New client from: " + client->peerAddress().toString());
              connect(client,SIGNAL(readyRead()),this,SLOT(readyRead())); //it does not work
              connect(client,SIGNAL(disconnected()),this,SLOT(disconnected()));
          
          }
          void HelloworldServer::disconnected(){
              QTcpSocket  *client= (QTcpSocket*) sender();
              qDebug()<<"Client disconnected :"<< client->peerAddress().toString();
              clients.remove(client);
          }
          

          void HelloworldServer::readyRead(){

          QTcpSocket * client = (QTcpSocket *)sender();
          client->open(QIODevice::ReadWrite);
          QByteArray data=client->readAll();
          client->waitForReadyRead(100);
          m_pHelloWindow->addMessage(" : " + client->write(data));
          
          }
          void HelloworldServer::incomingConnection(qintptr handle){
              QTcpSocket *client=new QTcpSocket(this);
              client->setSocketDescriptor(handle);
              clients.insert(client);
              m_pHelloWindow->addMessage("New client from: " + client->peerAddress().toString());
              connect(client,SIGNAL(readyRead()),this,SLOT(readyRead())); //bu satırın ha varlığı ha yokluğu
              connect(client,SIGNAL(disconnected()),this,SLOT(disconnected()));
          
          }
          void HelloworldServer::disconnected(){
              QTcpSocket  *client= (QTcpSocket*) sender();
              qDebug()<<"Client disconnected :"<< client->peerAddress().toString();
              clients.remove(client);
          }
          

          Here is the server.cpp

          #include "server.h"//MAIN WINDOW
          #include "ui_server.h"
          Server::Server(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::Server)
          {
              ui->setupUi(this);
          }
          
          Server::~Server()
          {
              delete ui;
          }
          
          void Server::addMessage(QString Msg){
              ui->textEditMessage->setText(Msg);
          }
          void Server::on_pushButtonStart_clicked()
          {
                m_pBoxServer= new HelloworldServer(this);
                bool success = m_pBoxServer->listen(QHostAddress::Any,quint16(ui->textEditPort->toPlainText().toInt()));
          
          
                //bool success = m_pBoxServer->listen(QHostAddress::Any,45444);
                if(!success){
                    addMessage("Server Failed");
                    qDebug()<<"server failed";
                }
                else{
                    addMessage("Server Started");
                }
          
          }
          

          What is the readyRead problem ? How can I solve readyRead() problem.

          J 1 Reply Last reply 12 May 2022, 12:27
          0
          • E ELIF
            12 May 2022, 11:32

            I get the value of IP adress in below code on textEditMesaage (ui)
            m_pHelloWindow->addMessage("New client from: " + client->peerAddress().toString()); //ITS OK
            BUT in below code

            readyRead() function is not working. I want to solve it I really struggle, so sad:(
            Here is helloworldserver.cpp

            #include "helloworldserver.h"
            #include "server.h"
            #include "server.cpp"
            #include "ui_server.h"
            #include<list>
            #include<string.h>
            #include <QList>
            #include "server.h"//MAIN WINDOW
            #include "ui_server.h"
            
            HelloworldServer::HelloworldServer(Server *pHelloServer,QObject *parent):QTcpServer(parent){
            
                m_pHelloWindow = pHelloServer;
            }
            
            void HelloworldServer::readyRead(){
            
                QTcpSocket * client = (QTcpSocket *)sender();
                client->open(QIODevice::ReadWrite);
                QByteArray data=client->readAll();
                client->waitForReadyRead(100);
                m_pHelloWindow->addMessage(" : " + client->write(data));
            
            }
            void HelloworldServer::incomingConnection(qintptr handle){
                QTcpSocket *client=new QTcpSocket(this);
                client->setSocketDescriptor(handle);
                clients.insert(client);
                m_pHelloWindow->addMessage("New client from: " + client->peerAddress().toString());
                connect(client,SIGNAL(readyRead()),this,SLOT(readyRead())); //it does not work
                connect(client,SIGNAL(disconnected()),this,SLOT(disconnected()));
            
            }
            void HelloworldServer::disconnected(){
                QTcpSocket  *client= (QTcpSocket*) sender();
                qDebug()<<"Client disconnected :"<< client->peerAddress().toString();
                clients.remove(client);
            }
            

            void HelloworldServer::readyRead(){

            QTcpSocket * client = (QTcpSocket *)sender();
            client->open(QIODevice::ReadWrite);
            QByteArray data=client->readAll();
            client->waitForReadyRead(100);
            m_pHelloWindow->addMessage(" : " + client->write(data));
            
            }
            void HelloworldServer::incomingConnection(qintptr handle){
                QTcpSocket *client=new QTcpSocket(this);
                client->setSocketDescriptor(handle);
                clients.insert(client);
                m_pHelloWindow->addMessage("New client from: " + client->peerAddress().toString());
                connect(client,SIGNAL(readyRead()),this,SLOT(readyRead())); //bu satırın ha varlığı ha yokluğu
                connect(client,SIGNAL(disconnected()),this,SLOT(disconnected()));
            
            }
            void HelloworldServer::disconnected(){
                QTcpSocket  *client= (QTcpSocket*) sender();
                qDebug()<<"Client disconnected :"<< client->peerAddress().toString();
                clients.remove(client);
            }
            

            Here is the server.cpp

            #include "server.h"//MAIN WINDOW
            #include "ui_server.h"
            Server::Server(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::Server)
            {
                ui->setupUi(this);
            }
            
            Server::~Server()
            {
                delete ui;
            }
            
            void Server::addMessage(QString Msg){
                ui->textEditMessage->setText(Msg);
            }
            void Server::on_pushButtonStart_clicked()
            {
                  m_pBoxServer= new HelloworldServer(this);
                  bool success = m_pBoxServer->listen(QHostAddress::Any,quint16(ui->textEditPort->toPlainText().toInt()));
            
            
                  //bool success = m_pBoxServer->listen(QHostAddress::Any,45444);
                  if(!success){
                      addMessage("Server Failed");
                      qDebug()<<"server failed";
                  }
                  else{
                      addMessage("Server Started");
                  }
            
            }
            

            What is the readyRead problem ? How can I solve readyRead() problem.

            J Offline
            J Offline
            JonB
            wrote on 12 May 2022, 12:27 last edited by JonB 5 Dec 2022, 13:02
            #5

            @ELIF said in Client To server message sending:

            readyRead() function is not working.

            That is not a description of a problem.

            I have no idea what you are trying to achieve, but readyRead() code looks quite wrong.

            Even if the sender is a QTcpSocket * how would client->open() make any sense here? If the client is not already open how would it send a readyRead() signal? I'm afraid all the code in that method looks wrong.

            You really need to follow @VRonin's link. Start by getting that working exactly as-is in the same code, not your own ideas; did you do that? When you have that working, adjust it a bit at a time to add whatever new/different features you want.

            J 1 Reply Last reply 12 May 2022, 14:13
            3
            • E Offline
              E Offline
              ELIF
              wrote on 12 May 2022, 14:12 last edited by
              #6

              @JonB said in Client To server message sending:

              Even if the sender is a QTcpSocket * how would client->open() make any sense here?

              I want to send message from client user interface to to server .

              1 Reply Last reply
              0
              • J JonB
                12 May 2022, 12:27

                @ELIF said in Client To server message sending:

                readyRead() function is not working.

                That is not a description of a problem.

                I have no idea what you are trying to achieve, but readyRead() code looks quite wrong.

                Even if the sender is a QTcpSocket * how would client->open() make any sense here? If the client is not already open how would it send a readyRead() signal? I'm afraid all the code in that method looks wrong.

                You really need to follow @VRonin's link. Start by getting that working exactly as-is in the same code, not your own ideas; did you do that? When you have that working, adjust it a bit at a time to add whatever new/different features you want.

                J Offline
                J Offline
                JonB
                wrote on 12 May 2022, 14:13 last edited by JonB 5 Dec 2022, 14:27
                #7

                @ELIF

                If the sender() to HelloworldServer::readyRead() is client, as per your code, I don't understand how client->open() would be appropriate. How is sender()/client() not already open if it has signalled readyRead()?

                @JonB said in Client To server message sending:

                You really need to follow @VRonin's link. Start by getting that working exactly as-is in the same code, not your own ideas; did you do that? When you have that working, adjust it a bit at a time to add whatever new/different features you want.

                Once you have understood that code and how it works you can doubtless make your own additions. If you do not understand how it works then you will struggle.

                Meanwhile, if I understand your code/intention correctly, what exactly should we understand "readyRead() function is not working. " to mean? What is not working, what happens/does not happen? To echo back, do the following lines suffice:

                QTcpSocket * client = static_cast<QTcpSocket *>(sender());
                QByteArray data = client->readAll();
                int count = client->write(data);
                m_pHelloWindow->addMessage(QString(" : %1").arg(count));
                
                1 Reply Last reply
                1
                • E Offline
                  E Offline
                  ELIF
                  wrote on 12 May 2022, 15:01 last edited by
                  #8

                  @JonB said in Client To server message sending:

                  le, if I understand your code/intention correctly, what exactly should we understand "readyRead() function is not working. " to mean? What is not working, what happens/does not happen? To echo back, do the following lines suffice:

                  readyRead function is for read message from server interface via socket .
                  If readyRead function run I will see the message that I write server textEdit.

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    ELIF
                    wrote on 12 May 2022, 15:01 last edited by
                    #9

                    Thank you so much:)

                    J 1 Reply Last reply 12 May 2022, 15:03
                    0
                    • E ELIF
                      12 May 2022, 15:01

                      Thank you so much:)

                      J Offline
                      J Offline
                      JonB
                      wrote on 12 May 2022, 15:03 last edited by
                      #10

                      @ELIF
                      I believe/hope the code I put above would work, but not sure if you are saying it does or still does not.

                      Meanwhile a new thread, https://forum.qt.io/topic/136465/sending-data-from-client-to-server, seems to have been opened by user @star673, and I have suggested there that is similar to what you are working on, and you two ought co-operate! I'm thinking the answers will be similar in both threads.

                      E 1 Reply Last reply 13 May 2022, 05:26
                      0
                      • J JonB
                        12 May 2022, 15:03

                        @ELIF
                        I believe/hope the code I put above would work, but not sure if you are saying it does or still does not.

                        Meanwhile a new thread, https://forum.qt.io/topic/136465/sending-data-from-client-to-server, seems to have been opened by user @star673, and I have suggested there that is similar to what you are working on, and you two ought co-operate! I'm thinking the answers will be similar in both threads.

                        E Offline
                        E Offline
                        ELIF
                        wrote on 13 May 2022, 05:26 last edited by
                        #11

                        @JonB said in Client To server message sending:

                        'm thinking the answers will be similar in both threads.

                        Yes we are co-worker.

                        J 1 Reply Last reply 13 May 2022, 07:58
                        0
                        • E ELIF
                          13 May 2022, 05:26

                          @JonB said in Client To server message sending:

                          'm thinking the answers will be similar in both threads.

                          Yes we are co-worker.

                          J Offline
                          J Offline
                          JonB
                          wrote on 13 May 2022, 07:58 last edited by JonB
                          #12

                          @ELIF
                          Then you might be better pooling your questions/comments into a single thread, rather than raising separate ones to ask the same thing and having people try to answer twice....

                          1 Reply Last reply
                          0

                          1/12

                          10 May 2022, 15:03

                          • Login

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