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. Sending Data from Client to Server
Forum Updated to NodeBB v4.3 + New Features

Sending Data from Client to Server

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 7 Posters 2.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.
  • S Offline
    S Offline
    star673
    wrote on 12 May 2022, 14:38 last edited by
    #1

    Hello all,

    I am trying to amke sever and client connection whiche enables to send data to both directions. It is a little demo that I am trying to understand the structure. I will send data from server to client but I cant send data from client to server. I have created two different console applications like ServerConsole and ClientConsole.

    My server.cpp file is below.

    #include "server.h"
    
    Server::Server()
    {
        tcpServer = new QTcpServer();
         connect(tcpServer,&QTcpServer::newConnection,this,&Server::newConnection);
         initServer();
    }
    
    void Server::incomingConnection(int socketfd){
        QTcpSocket *client=new QTcpSocket(this);
    //    client->setSocketDescriptor(socketfd);
    //    clients.insert(client);
    
        qDebug() << "New client from: " << client->peerAddress().toString();
    
        connect(client,SIGNAL(readyRead()),this,SLOT(readyRead()));
        connect(client,SIGNAL(disconnected()),this,SLOT(disconnected()));
    }
    
    void Server::newConnection(){
        qDebug() << "-------NEW CONNECTION SLOT--------";
        clientConnection = tcpServer->nextPendingConnection();
         connect(clientConnection, &QAbstractSocket::disconnected,
                 clientConnection, &QObject::deleteLater);
    
    
         clientConnection->write("ABCDE");
         //clientConnection->disconnectFromHost();
    }
    
    
    void Server::readyRead(){
       qDebug() << "|||||||||||READYREAD SLOT--------";
        qDebug() << clientConnection->readAll();
    }
    
    void Server::disconnected(){
        qDebug() << "//////////DISCONNECTED CONNECTION SLOT--------";
    }
    
    void Server::initServer(){
    
    if (!tcpServer->listen(QHostAddress::Any,45442)) {
        qDebug() << tcpServer->errorString();
    
    }
    }
    
    

    My server.h file is below.

    #ifndef SERVER_H
    #define SERVER_H
    
    
    #include <QObject>
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <QDebug>
    #include <QDataStream>
    #include <QByteArray>
    #include <QNetworkInterface>
    #include <QAbstractSocket>
    
    class Server : public QTcpServer
    {
    public:
        Server();
        QTcpServer *tcpServer = nullptr;
        void initServer();
        QTcpSocket *clientConnection;
    
    
    
    
    
    protected:
        void incomingConnection(int socketfd);
    
    public slots:
        void newConnection();
        void readyRead();
        void disconnected();
    private:
        QSet<QTcpSocket*> clients;
    };
    
    #endif // SERVER_H
    
    

    My main.cpp file for Server application is below.

    #include <QCoreApplication>
    #include "server.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        Server server;
    
        return a.exec();
    }
    
    

    My client.cpp is below.

    #include "client.h"
    
    Client::Client()
    {
        tcpSocket = new QTcpSocket();
        tcpSocket->connectToHost(QHostAddress::Any,45442);
        connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
        connect(tcpSocket, &QTcpSocket::connected, this, &Client::sendMessageToServer);
        connect(tcpSocket,&QTcpSocket::readyRead, this, &Client::readyRead);
    
    }
    
    void Client::displayError(QAbstractSocket::SocketError socketError){
    
        switch(socketError){
        case QAbstractSocket::RemoteHostClosedError:
            break;
    
        case QAbstractSocket::HostNotFoundError:
            qDebug() << "HostNotFoundError. Connection was refused. Server is not working";
            break;
    
    
        case QAbstractSocket::ConnectionRefusedError:
            qDebug() << "ConnectionRefusedError. Client. The host was not found";
            break;
    
        default:
            qDebug() << "ErrorString : " << tcpSocket->errorString();
      }
    
    }
    
    void Client::sendMessageToServer(){
        qDebug() << "INSIDe sendMessageToServer";
        tcpSocket->write("MessageFromClient");
        tcpSocket->flush();
    
    }
    
    void Client::readyRead(){
        qDebug() << "///READYREAD SLOT///";
        qDebug() << tcpSocket->readAll();
    
    
    }
    
    
    

    My client.h is below.

    #ifndef CLIENT_H
    #define CLIENT_H
    
    #include <QObject>
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <QDebug>
    #include <QDataStream>
    #include <QByteArray>
    #include <QNetworkInterface>
    #include <QAbstractSocket>
    
    class Client  : public QTcpSocket
    {
    public:
        Client();
        QTcpSocket *tcpSocket = nullptr;
    
    public slots:
        void displayError(QAbstractSocket::SocketError socketError);
        void sendMessageToServer();
        void readyRead();
    };
    
    #endif // CLIENT_H
    
    

    My main.cpp file for Client application is below.

    #include <QCoreApplication>
    #include "client.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        Client client;
    
        return a.exec();
    }
    
    

    I would like to send the data without disconnected from client or server. How can I sending data to both directions? Any help will be appreciated.

    JonBJ 1 Reply Last reply 12 May 2022, 15:00
    0
    • S star673
      16 May 2022, 18:48

      @Christian-Ehrlicher

      My server.cpp file is,

      #include "server.h"
      
      Server::Server(QObject *parent) : QObject(parent)
      {
          tcpServer = new QTcpServer();
           connect(tcpServer,&QTcpServer::newConnection,this,&Server::newConnection);
           initServer();
      }
      
      void Server::incomingConnection(int socketfd){
         // clientConnection=new QTcpSocket(this);
          clientConnection = tcpServer->nextPendingConnection();
           connect(clientConnection, &QAbstractSocket::disconnected,
                   clientConnection, &QObject::deleteLater);
      
      //    client->setSocketDescriptor(socketfd);
      //    clients.insert(client);
      
          qDebug() << "New client from: " << clientConnection->peerAddress().toString();
      
          connect(clientConnection,SIGNAL(readyRead()),this,SLOT(readyRead()));
          connect(clientConnection,SIGNAL(disconnected()),this,SLOT(disconnected()));
      }
      
      void Server::newConnection(){
          qDebug() << "-------NEW CONNECTION SLOT--------";
      
      //    QByteArray block;
      //    QDataStream out(&block, QIODevice::ReadWrite);
      //    out.setVersion(QDataStream::Qt_5_9);
      
      //    out << "ABCderfHt";
      
      
           clientConnection->write("ABCDE");
           //clientConnection->disconnectFromHost();
      }
      
      
      void Server::readyRead(){
         qDebug() << "|||||||||||READYREAD SLOT--------";
      
      
      
          qDebug() << clientConnection->readAll();
      }
      
      void Server::disconnected(){
          qDebug() << "//////////DISCONNECTED CONNECTION SLOT--------";
      }
      
      void Server::initServer(){
      
          if (!tcpServer->listen(QHostAddress::Any,45442)) {
              qDebug() << tcpServer->errorString();
      
          }
      
          else{
              qDebug() << "Listening";
          }
      }
      
      

      My server.h file is,

      #ifndef SERVER_H
      #define SERVER_H
      
      
      #include <QObject>
      #include <QTcpServer>
      #include <QTcpSocket>
      #include <QDebug>
      #include <QDataStream>
      #include <QByteArray>
      #include <QNetworkInterface>
      #include <QAbstractSocket>
      
      
      
      
      
      class Server : public QObject
      {
      
          Q_OBJECT
      
      public:
          explicit Server(QObject *parent = nullptr);
          QTcpServer *tcpServer = nullptr;
          void initServer();
          QTcpSocket *clientConnection;
         // QDataStream out;
      
      
      
      
      
      protected:
          void incomingConnection(int socketfd);
      
      public slots:
          void newConnection();
          void readyRead();
          void disconnected();
      private:
          QSet<QTcpSocket*> clients;
      };
      
      #endif // SERVER_H
      
      

      The main.cpp on the server side,

      #include <QCoreApplication>
      #include "server.h"
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          Server server;
      
          return a.exec();
      }
      
      

      My client.cpp is,

      #include "client.h"
      
      Client::Client()
      {
          tcpSocket = new QTcpSocket();
          tcpSocket->connectToHost(QHostAddress::LocalHost,45442);
          //connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
         // connect(tcpSocket,QOverload<QAbstractSocket::SocketError>::of(error),this,&Client::displayError);
          connect(tcpSocket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
              [=](QAbstractSocket::SocketError socketError){
      
              switch(socketError){
              case QAbstractSocket::RemoteHostClosedError:
                  break;
      
              case QAbstractSocket::HostNotFoundError:
                  qDebug() << "HostNotFoundError. Connection was refused. Server is not working";
                  break;
      
      
              case QAbstractSocket::ConnectionRefusedError:
                  qDebug() << "ConnectionRefusedError. Client. The host was not found";
                  break;
      
              default:
                  qDebug() << "ErrorString : " << tcpSocket->errorString();
            }
      
      
          });
      
      
          connect(tcpSocket, &QTcpSocket::connected, this, &Client::sendMessageToServer);
          connect(tcpSocket,&QTcpSocket::readyRead, this, &Client::readyRead);
      
      }
      
      void Client::displayError(QAbstractSocket::SocketError socketError){
      
          switch(socketError){
          case QAbstractSocket::RemoteHostClosedError:
              break;
      
          case QAbstractSocket::HostNotFoundError:
              qDebug() << "HostNotFoundError. Connection was refused. Server is not working";
              break;
      
      
          case QAbstractSocket::ConnectionRefusedError:
              qDebug() << "ConnectionRefusedError. Client. The host was not found";
              break;
      
          default:
              qDebug() << "ErrorString : " << tcpSocket->errorString();
        }
      
      }
      
      void Client::sendMessageToServer(){
          qDebug() << "INSIDe sendMessageToServer";
          //tcpSocket->waitForReadyRead();
          qDebug() << tcpSocket->canReadLine() << tcpSocket->readAll();
          tcpSocket->write("MessageFromClient");
          //tcpSocket->flush();
      
      }
      
      void Client::readyRead(){
          qDebug() << "///READYREAD SLOT///";
      
      //    in.startTransaction();
      
      //    QString nextFortune;
      //    in >> nextFortune;
      
      //    if (!in.commitTransaction())
      //        return;
          qDebug() << tcpSocket->readAll();
      
      
      }
      
      
      

      My client.h is,

      #ifndef CLIENT_H
      #define CLIENT_H
      
      #include <QObject>
      #include <QTcpServer>
      #include <QTcpSocket>
      #include <QDebug>
      #include <QDataStream>
      #include <QByteArray>
      #include <QNetworkInterface>
      #include <QAbstractSocket>
      
      class Client  : public QTcpSocket
      {
      public:
          Client();
          QTcpSocket *tcpSocket = nullptr;
           QDataStream in;
      
      public slots:
          void displayError(QAbstractSocket::SocketError socketError);
          void sendMessageToServer();
          void readyRead();
      };
      
      #endif // CLIENT_H
      
      

      The main.cpp on the client side is,

      #include <QCoreApplication>
      #include "client.h"
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          Client client;
      
          return a.exec();
      }
      
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 17 May 2022, 05:38 last edited by
      #21

      @star673 said in Sending Data from Client to Server:

      void Server::incomingConnection(int socketfd){

      If you check the documentation you will see that incomingConnection is a protected virtual method in QTcpServer. If you want to use it you have to subclass QTcpServer and override it. But if you use QTcpServer::newConnection signal there is no need for incomingConnection - simply move the code from your incomingConnection to newConnection.

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

      S 1 Reply Last reply 17 May 2022, 10:23
      1
      • S star673
        12 May 2022, 14:38

        Hello all,

        I am trying to amke sever and client connection whiche enables to send data to both directions. It is a little demo that I am trying to understand the structure. I will send data from server to client but I cant send data from client to server. I have created two different console applications like ServerConsole and ClientConsole.

        My server.cpp file is below.

        #include "server.h"
        
        Server::Server()
        {
            tcpServer = new QTcpServer();
             connect(tcpServer,&QTcpServer::newConnection,this,&Server::newConnection);
             initServer();
        }
        
        void Server::incomingConnection(int socketfd){
            QTcpSocket *client=new QTcpSocket(this);
        //    client->setSocketDescriptor(socketfd);
        //    clients.insert(client);
        
            qDebug() << "New client from: " << client->peerAddress().toString();
        
            connect(client,SIGNAL(readyRead()),this,SLOT(readyRead()));
            connect(client,SIGNAL(disconnected()),this,SLOT(disconnected()));
        }
        
        void Server::newConnection(){
            qDebug() << "-------NEW CONNECTION SLOT--------";
            clientConnection = tcpServer->nextPendingConnection();
             connect(clientConnection, &QAbstractSocket::disconnected,
                     clientConnection, &QObject::deleteLater);
        
        
             clientConnection->write("ABCDE");
             //clientConnection->disconnectFromHost();
        }
        
        
        void Server::readyRead(){
           qDebug() << "|||||||||||READYREAD SLOT--------";
            qDebug() << clientConnection->readAll();
        }
        
        void Server::disconnected(){
            qDebug() << "//////////DISCONNECTED CONNECTION SLOT--------";
        }
        
        void Server::initServer(){
        
        if (!tcpServer->listen(QHostAddress::Any,45442)) {
            qDebug() << tcpServer->errorString();
        
        }
        }
        
        

        My server.h file is below.

        #ifndef SERVER_H
        #define SERVER_H
        
        
        #include <QObject>
        #include <QTcpServer>
        #include <QTcpSocket>
        #include <QDebug>
        #include <QDataStream>
        #include <QByteArray>
        #include <QNetworkInterface>
        #include <QAbstractSocket>
        
        class Server : public QTcpServer
        {
        public:
            Server();
            QTcpServer *tcpServer = nullptr;
            void initServer();
            QTcpSocket *clientConnection;
        
        
        
        
        
        protected:
            void incomingConnection(int socketfd);
        
        public slots:
            void newConnection();
            void readyRead();
            void disconnected();
        private:
            QSet<QTcpSocket*> clients;
        };
        
        #endif // SERVER_H
        
        

        My main.cpp file for Server application is below.

        #include <QCoreApplication>
        #include "server.h"
        
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
        
            Server server;
        
            return a.exec();
        }
        
        

        My client.cpp is below.

        #include "client.h"
        
        Client::Client()
        {
            tcpSocket = new QTcpSocket();
            tcpSocket->connectToHost(QHostAddress::Any,45442);
            connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
            connect(tcpSocket, &QTcpSocket::connected, this, &Client::sendMessageToServer);
            connect(tcpSocket,&QTcpSocket::readyRead, this, &Client::readyRead);
        
        }
        
        void Client::displayError(QAbstractSocket::SocketError socketError){
        
            switch(socketError){
            case QAbstractSocket::RemoteHostClosedError:
                break;
        
            case QAbstractSocket::HostNotFoundError:
                qDebug() << "HostNotFoundError. Connection was refused. Server is not working";
                break;
        
        
            case QAbstractSocket::ConnectionRefusedError:
                qDebug() << "ConnectionRefusedError. Client. The host was not found";
                break;
        
            default:
                qDebug() << "ErrorString : " << tcpSocket->errorString();
          }
        
        }
        
        void Client::sendMessageToServer(){
            qDebug() << "INSIDe sendMessageToServer";
            tcpSocket->write("MessageFromClient");
            tcpSocket->flush();
        
        }
        
        void Client::readyRead(){
            qDebug() << "///READYREAD SLOT///";
            qDebug() << tcpSocket->readAll();
        
        
        }
        
        
        

        My client.h is below.

        #ifndef CLIENT_H
        #define CLIENT_H
        
        #include <QObject>
        #include <QTcpServer>
        #include <QTcpSocket>
        #include <QDebug>
        #include <QDataStream>
        #include <QByteArray>
        #include <QNetworkInterface>
        #include <QAbstractSocket>
        
        class Client  : public QTcpSocket
        {
        public:
            Client();
            QTcpSocket *tcpSocket = nullptr;
        
        public slots:
            void displayError(QAbstractSocket::SocketError socketError);
            void sendMessageToServer();
            void readyRead();
        };
        
        #endif // CLIENT_H
        
        

        My main.cpp file for Client application is below.

        #include <QCoreApplication>
        #include "client.h"
        
        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
            Client client;
        
            return a.exec();
        }
        
        

        I would like to send the data without disconnected from client or server. How can I sending data to both directions? Any help will be appreciated.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 12 May 2022, 15:00 last edited by
        #2

        @star673
        @ELIF in thread https://forum.qt.io/topic/136412/client-to-server-message-sending seems to be trying to do the same thing as you? You two should co-operate!

        I admit I have not tried/tested anything in this area, but what exactly is the problem? So far as I know sockets are open read/write, so in principle either side can just read from/write to their end of the socket and the reverse at the other side and it should just "work"? Unless someone says I am mistaken....

        S 1 Reply Last reply 13 May 2022, 05:36
        0
        • JonBJ JonB
          12 May 2022, 15:00

          @star673
          @ELIF in thread https://forum.qt.io/topic/136412/client-to-server-message-sending seems to be trying to do the same thing as you? You two should co-operate!

          I admit I have not tried/tested anything in this area, but what exactly is the problem? So far as I know sockets are open read/write, so in principle either side can just read from/write to their end of the socket and the reverse at the other side and it should just "work"? Unless someone says I am mistaken....

          S Offline
          S Offline
          star673
          wrote on 13 May 2022, 05:36 last edited by
          #3

          @JonB
          Unfortubately, I cant send data from client to server. Can you have a chance to execute the applications that I send it?
          It shouldn't be any problem as I know like you said. There might be a socket problem in my case but I could not get it exactly.

          jsulmJ 1 Reply Last reply 13 May 2022, 05:44
          0
          • S star673
            13 May 2022, 05:36

            @JonB
            Unfortubately, I cant send data from client to server. Can you have a chance to execute the applications that I send it?
            It shouldn't be any problem as I know like you said. There might be a socket problem in my case but I could not get it exactly.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on 13 May 2022, 05:44 last edited by
            #4

            @star673 But what exactly does not work?
            Can your client connect to the server?
            Do you see incoming connection on the server?

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

            S 1 Reply Last reply 13 May 2022, 08:29
            0
            • E Offline
              E Offline
              ELIF
              wrote on 13 May 2022, 05:47 last edited by
              #5

              Yes we see incoming connection on server side.There is a successful connection.

              But we can not see the message on server ui . We should see the message on server part

              jsulmJ 1 Reply Last reply 13 May 2022, 05:53
              0
              • E ELIF
                13 May 2022, 05:47

                Yes we see incoming connection on server side.There is a successful connection.

                But we can not see the message on server ui . We should see the message on server part

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 13 May 2022, 05:53 last edited by jsulm
                #6

                @ELIF Your server code is wrong. You need to connect a slot to readyRead signal from clientConnection! clientConnection is the socket to communicate with the client. QTcpServer has no readyRead signal (it would not make sense to have it as QTcpServer can manage many connections at the same time).

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

                1 Reply Last reply
                1
                • E Offline
                  E Offline
                  ELIF
                  wrote on 13 May 2022, 06:28 last edited by
                  #7

                  @jsulm said in Sending Data from Client to Server:

                  Your server code is wrong

                  Which one ? would yo show wrong codes ?
                  I understand somehow.

                  Thank you.

                  KroMignonK jsulmJ 2 Replies Last reply 13 May 2022, 06:39
                  0
                  • E ELIF
                    13 May 2022, 06:28

                    @jsulm said in Sending Data from Client to Server:

                    Your server code is wrong

                    Which one ? would yo show wrong codes ?
                    I understand somehow.

                    Thank you.

                    KroMignonK Offline
                    KroMignonK Offline
                    KroMignon
                    wrote on 13 May 2022, 06:39 last edited by
                    #8

                    @ELIF said in Sending Data from Client to Server:

                    Which one ? would yo show wrong codes ?

                    There are many errors on it:

                    • first, why do you sub-classing QTcpServer and not QObject ?
                    • second, to be able to define signals/slots you must add Q_OBJECT in you class header

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    1 Reply Last reply
                    2
                    • E ELIF
                      13 May 2022, 06:28

                      @jsulm said in Sending Data from Client to Server:

                      Your server code is wrong

                      Which one ? would yo show wrong codes ?
                      I understand somehow.

                      Thank you.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 13 May 2022, 06:44 last edited by jsulm
                      #9

                      @ELIF said in Sending Data from Client to Server:

                      Which one ? would yo show wrong codes ?

                      I already said what is wrong: "You need to connect a slot to readyRead signal from clientConnection!"
                      Also comment from @KroMignon is correct: it makes no sense to subclass QTcpServer and then add a QTcpServer member variable...

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

                      1 Reply Last reply
                      0
                      • jsulmJ jsulm
                        13 May 2022, 05:44

                        @star673 But what exactly does not work?
                        Can your client connect to the server?
                        Do you see incoming connection on the server?

                        S Offline
                        S Offline
                        star673
                        wrote on 13 May 2022, 08:29 last edited by
                        #10

                        I got an output in server side is like:
                        -------NEW CONNECTION SLOT--------
                        Press <RETURN> to close this window...

                        Inclient side is the oput like:
                        QObject::connect: No such slot QTcpSocket::displayError(QAbstractSocket::SocketError) in ../ClientConsole/client.cpp:7
                        INSIDe sendMessageToServer

                        I have changed the server.cpp is like below. Like you said, I have used clientConnection object for readyRead slot. The connection has accomplished but I couldnt get the data.

                        #include "server.h"
                        
                        Server::Server(QObject *parent) : QObject(parent)
                        {
                            tcpServer = new QTcpServer();
                             connect(tcpServer,&QTcpServer::newConnection,this,&Server::newConnection);
                             initServer();
                        }
                        
                        void Server::incomingConnection(int socketfd){
                            clientConnection=new QTcpSocket(this);
                            clientConnection = tcpServer->nextPendingConnection();
                             connect(clientConnection, &QAbstractSocket::disconnected,
                                     clientConnection, &QObject::deleteLater);
                        //    client->setSocketDescriptor(socketfd);
                        //    clients.insert(client);
                        
                            qDebug() << "New client from: " << clientConnection->peerAddress().toString();
                        
                            connect(clientConnection,SIGNAL(readyRead()),this,SLOT(readyRead()));
                            connect(clientConnection,SIGNAL(disconnected()),this,SLOT(disconnected()));
                        }
                        
                        void Server::newConnection(){
                            qDebug() << "-------NEW CONNECTION SLOT--------";
                        
                        
                        
                             clientConnection->write("ABCDE");
                             //clientConnection->disconnectFromHost();
                        }
                        
                        
                        void Server::readyRead(){
                           qDebug() << "|||||||||||READYREAD SLOT--------";
                            qDebug() << clientConnection->readAll();
                        }
                        
                        void Server::disconnected(){
                            qDebug() << "//////////DISCONNECTED CONNECTION SLOT--------";
                        }
                        
                        void Server::initServer(){
                        
                        if (!tcpServer->listen(QHostAddress::Any,45442)) {
                            qDebug() << "AAAA";
                            qDebug() << tcpServer->errorString();
                        
                        }
                        }
                        
                        
                        JonBJ 1 Reply Last reply 13 May 2022, 09:09
                        0
                        • S star673
                          13 May 2022, 08:29

                          I got an output in server side is like:
                          -------NEW CONNECTION SLOT--------
                          Press <RETURN> to close this window...

                          Inclient side is the oput like:
                          QObject::connect: No such slot QTcpSocket::displayError(QAbstractSocket::SocketError) in ../ClientConsole/client.cpp:7
                          INSIDe sendMessageToServer

                          I have changed the server.cpp is like below. Like you said, I have used clientConnection object for readyRead slot. The connection has accomplished but I couldnt get the data.

                          #include "server.h"
                          
                          Server::Server(QObject *parent) : QObject(parent)
                          {
                              tcpServer = new QTcpServer();
                               connect(tcpServer,&QTcpServer::newConnection,this,&Server::newConnection);
                               initServer();
                          }
                          
                          void Server::incomingConnection(int socketfd){
                              clientConnection=new QTcpSocket(this);
                              clientConnection = tcpServer->nextPendingConnection();
                               connect(clientConnection, &QAbstractSocket::disconnected,
                                       clientConnection, &QObject::deleteLater);
                          //    client->setSocketDescriptor(socketfd);
                          //    clients.insert(client);
                          
                              qDebug() << "New client from: " << clientConnection->peerAddress().toString();
                          
                              connect(clientConnection,SIGNAL(readyRead()),this,SLOT(readyRead()));
                              connect(clientConnection,SIGNAL(disconnected()),this,SLOT(disconnected()));
                          }
                          
                          void Server::newConnection(){
                              qDebug() << "-------NEW CONNECTION SLOT--------";
                          
                          
                          
                               clientConnection->write("ABCDE");
                               //clientConnection->disconnectFromHost();
                          }
                          
                          
                          void Server::readyRead(){
                             qDebug() << "|||||||||||READYREAD SLOT--------";
                              qDebug() << clientConnection->readAll();
                          }
                          
                          void Server::disconnected(){
                              qDebug() << "//////////DISCONNECTED CONNECTION SLOT--------";
                          }
                          
                          void Server::initServer(){
                          
                          if (!tcpServer->listen(QHostAddress::Any,45442)) {
                              qDebug() << "AAAA";
                              qDebug() << tcpServer->errorString();
                          
                          }
                          }
                          
                          
                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on 13 May 2022, 09:09 last edited by JonB
                          #11

                          @star673 said in Sending Data from Client to Server:

                          clientConnection=new QTcpSocket(this);
                          clientConnection = tcpServer->nextPendingConnection();
                          

                          Why do you have that first line here? All it does is leak a new QTcpSocket.

                          The connection has accomplished but I couldnt get the data.

                          Is your Server::readyRead() called at all?

                          Inclient side is the oput like:
                          QObject::connect: No such slot QTcpSocket::displayError(QAbstractSocket::SocketError) in ../ClientConsole/client.cpp:7
                          INSIDe sendMessageToServer

                          Then you have an error in client.cpp, which has nothing to do with the server code you are showing....

                          connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
                          connect(tcpSocket, &QTcpSocket::connected, this, &Client::sendMessageToServer);
                          

                          You would not get runtime errors if you made all your connections use the new style connect syntax, like the second line here. Get rid of all your SIGNAL/SLOT() macro connections, everywhere, and see where you are.

                          Press <RETURN> to close this window...

                          This looks like the server program has exited? Use a debugger/debug statements to trace what is happening in the server.

                          S JonBJ 3 Replies Last reply 13 May 2022, 09:33
                          1
                          • JonBJ JonB
                            13 May 2022, 09:09

                            @star673 said in Sending Data from Client to Server:

                            clientConnection=new QTcpSocket(this);
                            clientConnection = tcpServer->nextPendingConnection();
                            

                            Why do you have that first line here? All it does is leak a new QTcpSocket.

                            The connection has accomplished but I couldnt get the data.

                            Is your Server::readyRead() called at all?

                            Inclient side is the oput like:
                            QObject::connect: No such slot QTcpSocket::displayError(QAbstractSocket::SocketError) in ../ClientConsole/client.cpp:7
                            INSIDe sendMessageToServer

                            Then you have an error in client.cpp, which has nothing to do with the server code you are showing....

                            connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
                            connect(tcpSocket, &QTcpSocket::connected, this, &Client::sendMessageToServer);
                            

                            You would not get runtime errors if you made all your connections use the new style connect syntax, like the second line here. Get rid of all your SIGNAL/SLOT() macro connections, everywhere, and see where you are.

                            Press <RETURN> to close this window...

                            This looks like the server program has exited? Use a debugger/debug statements to trace what is happening in the server.

                            S Offline
                            S Offline
                            star673
                            wrote on 13 May 2022, 09:33 last edited by
                            #12

                            @JonB
                            I have commented this line.

                            clientConnection=new QTcpSocket(this);
                            

                            When I commented the connect functions server application did not crashed. It seems the problem is related with these lines. Did I use them worng?

                                connect(clientConnection,SIGNAL(readyRead()),this,SLOT(readyRead()));
                                connect(clientConnection,SIGNAL(disconnected()),this,SLOT(disconnected()));
                            
                            1 Reply Last reply
                            0
                            • JonBJ JonB
                              13 May 2022, 09:09

                              @star673 said in Sending Data from Client to Server:

                              clientConnection=new QTcpSocket(this);
                              clientConnection = tcpServer->nextPendingConnection();
                              

                              Why do you have that first line here? All it does is leak a new QTcpSocket.

                              The connection has accomplished but I couldnt get the data.

                              Is your Server::readyRead() called at all?

                              Inclient side is the oput like:
                              QObject::connect: No such slot QTcpSocket::displayError(QAbstractSocket::SocketError) in ../ClientConsole/client.cpp:7
                              INSIDe sendMessageToServer

                              Then you have an error in client.cpp, which has nothing to do with the server code you are showing....

                              connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
                              connect(tcpSocket, &QTcpSocket::connected, this, &Client::sendMessageToServer);
                              

                              You would not get runtime errors if you made all your connections use the new style connect syntax, like the second line here. Get rid of all your SIGNAL/SLOT() macro connections, everywhere, and see where you are.

                              Press <RETURN> to close this window...

                              This looks like the server program has exited? Use a debugger/debug statements to trace what is happening in the server.

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on 13 May 2022, 09:35 last edited by JonB
                              #13

                              @star673

                              @JonB said in Sending Data from Client to Server:

                              Get rid of all your SIGNAL/SLOT() macro connections, everywhere, and see where you are.

                              Come back when you have done that....

                              [I can see one thing which looks wrong at both your client & server sides, let's see whether it gets picked up. In any case, and especially with the trouble you are having, you really do need to change over to new style connects for all your code.]

                              H S 2 Replies Last reply 13 May 2022, 21:19
                              0
                              • JonBJ JonB
                                13 May 2022, 09:35

                                @star673

                                @JonB said in Sending Data from Client to Server:

                                Get rid of all your SIGNAL/SLOT() macro connections, everywhere, and see where you are.

                                Come back when you have done that....

                                [I can see one thing which looks wrong at both your client & server sides, let's see whether it gets picked up. In any case, and especially with the trouble you are having, you really do need to change over to new style connects for all your code.]

                                H Offline
                                H Offline
                                hix_boz
                                wrote on 13 May 2022, 21:19 last edited by
                                #14
                                This post is deleted!
                                1 Reply Last reply
                                0
                                • JonBJ JonB
                                  13 May 2022, 09:35

                                  @star673

                                  @JonB said in Sending Data from Client to Server:

                                  Get rid of all your SIGNAL/SLOT() macro connections, everywhere, and see where you are.

                                  Come back when you have done that....

                                  [I can see one thing which looks wrong at both your client & server sides, let's see whether it gets picked up. In any case, and especially with the trouble you are having, you really do need to change over to new style connects for all your code.]

                                  S Offline
                                  S Offline
                                  star673
                                  wrote on 13 May 2022, 21:24 last edited by
                                  #15

                                  @JonB

                                  How to change this line?

                                  connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
                                  
                                  JonBJ Christian EhrlicherC 2 Replies Last reply 13 May 2022, 22:53
                                  0
                                  • S star673
                                    13 May 2022, 21:24

                                    @JonB

                                    How to change this line?

                                    connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
                                    
                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on 13 May 2022, 22:53 last edited by JonB
                                    #16

                                    @star673

                                    connect(tcpSocket, &QTcpSocket::error, this, &ThisClass::displayError);
                                    

                                    UPDATE
                                    As per @Christian-Ehrlicher's link below, the QTcpSocket::error signal is overloaded in this class and you will actually need the QOverload<QAbstractSocket::SocketError>::of() shown there. You will also note that this method is now obsolete and you are advised to "Use errorOccurred() instead", which will look simpler.

                                    1 Reply Last reply
                                    0
                                    • S star673
                                      13 May 2022, 21:24

                                      @JonB

                                      How to change this line?

                                      connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
                                      
                                      Christian EhrlicherC Offline
                                      Christian EhrlicherC Offline
                                      Christian Ehrlicher
                                      Lifetime Qt Champion
                                      wrote on 14 May 2022, 07:28 last edited by
                                      #17

                                      @star673 said in Sending Data from Client to Server:

                                      How to change this line?

                                      By looking into the documentation... reading seems to be very hard.

                                      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
                                      2
                                      • JonBJ JonB
                                        13 May 2022, 09:09

                                        @star673 said in Sending Data from Client to Server:

                                        clientConnection=new QTcpSocket(this);
                                        clientConnection = tcpServer->nextPendingConnection();
                                        

                                        Why do you have that first line here? All it does is leak a new QTcpSocket.

                                        The connection has accomplished but I couldnt get the data.

                                        Is your Server::readyRead() called at all?

                                        Inclient side is the oput like:
                                        QObject::connect: No such slot QTcpSocket::displayError(QAbstractSocket::SocketError) in ../ClientConsole/client.cpp:7
                                        INSIDe sendMessageToServer

                                        Then you have an error in client.cpp, which has nothing to do with the server code you are showing....

                                        connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
                                        connect(tcpSocket, &QTcpSocket::connected, this, &Client::sendMessageToServer);
                                        

                                        You would not get runtime errors if you made all your connections use the new style connect syntax, like the second line here. Get rid of all your SIGNAL/SLOT() macro connections, everywhere, and see where you are.

                                        Press <RETURN> to close this window...

                                        This looks like the server program has exited? Use a debugger/debug statements to trace what is happening in the server.

                                        S Offline
                                        S Offline
                                        star673
                                        wrote on 16 May 2022, 10:47 last edited by star673
                                        #18

                                        @JonB

                                        I hav changed the error slot like below.

                                            connect(tcpSocket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
                                                [=](QAbstractSocket::SocketError socketError){
                                        
                                                switch(socketError){
                                                case QAbstractSocket::RemoteHostClosedError:
                                                    break;
                                        
                                                case QAbstractSocket::HostNotFoundError:
                                                    qDebug() << "HostNotFoundError. Connection was refused. Server is not working";
                                                    break;
                                        
                                        
                                                case QAbstractSocket::ConnectionRefusedError:
                                                    qDebug() << "ConnectionRefusedError. Client. The host was not found";
                                                    break;
                                        
                                                default:
                                                    qDebug() << "ErrorString : " << tcpSocket->errorString();
                                              }
                                        
                                        
                                            });
                                        

                                        Now I dont have any runtime error like before. The connected signal is emitted by the client but than nothing happens. I could not read from client, too. There is no printed socket error. My incoming connection function like below.

                                        void Server::incomingConnection(int socketfd){
                                           // clientConnection=new QTcpSocket(this);
                                            clientConnection = tcpServer->nextPendingConnection();
                                             connect(clientConnection, &QAbstractSocket::disconnected,
                                                     clientConnection, &QObject::deleteLater);
                                        
                                        //    client->setSocketDescriptor(socketfd);
                                        //    clients.insert(client);
                                        
                                            qDebug() << "New client from: " << clientConnection->peerAddress().toString();
                                        

                                        There is no print which starting "New client from" so it seems something wron with server side.

                                        1 Reply Last reply
                                        0
                                        • Christian EhrlicherC Offline
                                          Christian EhrlicherC Offline
                                          Christian Ehrlicher
                                          Lifetime Qt Champion
                                          wrote on 16 May 2022, 10:52 last edited by
                                          #19

                                          Please show your actual code with the new signal/slot syntax.

                                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                          Visit the Qt Academy at https://academy.qt.io/catalog

                                          S 1 Reply Last reply 16 May 2022, 18:48
                                          0
                                          • Christian EhrlicherC Christian Ehrlicher
                                            16 May 2022, 10:52

                                            Please show your actual code with the new signal/slot syntax.

                                            S Offline
                                            S Offline
                                            star673
                                            wrote on 16 May 2022, 18:48 last edited by
                                            #20

                                            @Christian-Ehrlicher

                                            My server.cpp file is,

                                            #include "server.h"
                                            
                                            Server::Server(QObject *parent) : QObject(parent)
                                            {
                                                tcpServer = new QTcpServer();
                                                 connect(tcpServer,&QTcpServer::newConnection,this,&Server::newConnection);
                                                 initServer();
                                            }
                                            
                                            void Server::incomingConnection(int socketfd){
                                               // clientConnection=new QTcpSocket(this);
                                                clientConnection = tcpServer->nextPendingConnection();
                                                 connect(clientConnection, &QAbstractSocket::disconnected,
                                                         clientConnection, &QObject::deleteLater);
                                            
                                            //    client->setSocketDescriptor(socketfd);
                                            //    clients.insert(client);
                                            
                                                qDebug() << "New client from: " << clientConnection->peerAddress().toString();
                                            
                                                connect(clientConnection,SIGNAL(readyRead()),this,SLOT(readyRead()));
                                                connect(clientConnection,SIGNAL(disconnected()),this,SLOT(disconnected()));
                                            }
                                            
                                            void Server::newConnection(){
                                                qDebug() << "-------NEW CONNECTION SLOT--------";
                                            
                                            //    QByteArray block;
                                            //    QDataStream out(&block, QIODevice::ReadWrite);
                                            //    out.setVersion(QDataStream::Qt_5_9);
                                            
                                            //    out << "ABCderfHt";
                                            
                                            
                                                 clientConnection->write("ABCDE");
                                                 //clientConnection->disconnectFromHost();
                                            }
                                            
                                            
                                            void Server::readyRead(){
                                               qDebug() << "|||||||||||READYREAD SLOT--------";
                                            
                                            
                                            
                                                qDebug() << clientConnection->readAll();
                                            }
                                            
                                            void Server::disconnected(){
                                                qDebug() << "//////////DISCONNECTED CONNECTION SLOT--------";
                                            }
                                            
                                            void Server::initServer(){
                                            
                                                if (!tcpServer->listen(QHostAddress::Any,45442)) {
                                                    qDebug() << tcpServer->errorString();
                                            
                                                }
                                            
                                                else{
                                                    qDebug() << "Listening";
                                                }
                                            }
                                            
                                            

                                            My server.h file is,

                                            #ifndef SERVER_H
                                            #define SERVER_H
                                            
                                            
                                            #include <QObject>
                                            #include <QTcpServer>
                                            #include <QTcpSocket>
                                            #include <QDebug>
                                            #include <QDataStream>
                                            #include <QByteArray>
                                            #include <QNetworkInterface>
                                            #include <QAbstractSocket>
                                            
                                            
                                            
                                            
                                            
                                            class Server : public QObject
                                            {
                                            
                                                Q_OBJECT
                                            
                                            public:
                                                explicit Server(QObject *parent = nullptr);
                                                QTcpServer *tcpServer = nullptr;
                                                void initServer();
                                                QTcpSocket *clientConnection;
                                               // QDataStream out;
                                            
                                            
                                            
                                            
                                            
                                            protected:
                                                void incomingConnection(int socketfd);
                                            
                                            public slots:
                                                void newConnection();
                                                void readyRead();
                                                void disconnected();
                                            private:
                                                QSet<QTcpSocket*> clients;
                                            };
                                            
                                            #endif // SERVER_H
                                            
                                            

                                            The main.cpp on the server side,

                                            #include <QCoreApplication>
                                            #include "server.h"
                                            
                                            int main(int argc, char *argv[])
                                            {
                                                QCoreApplication a(argc, argv);
                                            
                                                Server server;
                                            
                                                return a.exec();
                                            }
                                            
                                            

                                            My client.cpp is,

                                            #include "client.h"
                                            
                                            Client::Client()
                                            {
                                                tcpSocket = new QTcpSocket();
                                                tcpSocket->connectToHost(QHostAddress::LocalHost,45442);
                                                //connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError)));
                                               // connect(tcpSocket,QOverload<QAbstractSocket::SocketError>::of(error),this,&Client::displayError);
                                                connect(tcpSocket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
                                                    [=](QAbstractSocket::SocketError socketError){
                                            
                                                    switch(socketError){
                                                    case QAbstractSocket::RemoteHostClosedError:
                                                        break;
                                            
                                                    case QAbstractSocket::HostNotFoundError:
                                                        qDebug() << "HostNotFoundError. Connection was refused. Server is not working";
                                                        break;
                                            
                                            
                                                    case QAbstractSocket::ConnectionRefusedError:
                                                        qDebug() << "ConnectionRefusedError. Client. The host was not found";
                                                        break;
                                            
                                                    default:
                                                        qDebug() << "ErrorString : " << tcpSocket->errorString();
                                                  }
                                            
                                            
                                                });
                                            
                                            
                                                connect(tcpSocket, &QTcpSocket::connected, this, &Client::sendMessageToServer);
                                                connect(tcpSocket,&QTcpSocket::readyRead, this, &Client::readyRead);
                                            
                                            }
                                            
                                            void Client::displayError(QAbstractSocket::SocketError socketError){
                                            
                                                switch(socketError){
                                                case QAbstractSocket::RemoteHostClosedError:
                                                    break;
                                            
                                                case QAbstractSocket::HostNotFoundError:
                                                    qDebug() << "HostNotFoundError. Connection was refused. Server is not working";
                                                    break;
                                            
                                            
                                                case QAbstractSocket::ConnectionRefusedError:
                                                    qDebug() << "ConnectionRefusedError. Client. The host was not found";
                                                    break;
                                            
                                                default:
                                                    qDebug() << "ErrorString : " << tcpSocket->errorString();
                                              }
                                            
                                            }
                                            
                                            void Client::sendMessageToServer(){
                                                qDebug() << "INSIDe sendMessageToServer";
                                                //tcpSocket->waitForReadyRead();
                                                qDebug() << tcpSocket->canReadLine() << tcpSocket->readAll();
                                                tcpSocket->write("MessageFromClient");
                                                //tcpSocket->flush();
                                            
                                            }
                                            
                                            void Client::readyRead(){
                                                qDebug() << "///READYREAD SLOT///";
                                            
                                            //    in.startTransaction();
                                            
                                            //    QString nextFortune;
                                            //    in >> nextFortune;
                                            
                                            //    if (!in.commitTransaction())
                                            //        return;
                                                qDebug() << tcpSocket->readAll();
                                            
                                            
                                            }
                                            
                                            
                                            

                                            My client.h is,

                                            #ifndef CLIENT_H
                                            #define CLIENT_H
                                            
                                            #include <QObject>
                                            #include <QTcpServer>
                                            #include <QTcpSocket>
                                            #include <QDebug>
                                            #include <QDataStream>
                                            #include <QByteArray>
                                            #include <QNetworkInterface>
                                            #include <QAbstractSocket>
                                            
                                            class Client  : public QTcpSocket
                                            {
                                            public:
                                                Client();
                                                QTcpSocket *tcpSocket = nullptr;
                                                 QDataStream in;
                                            
                                            public slots:
                                                void displayError(QAbstractSocket::SocketError socketError);
                                                void sendMessageToServer();
                                                void readyRead();
                                            };
                                            
                                            #endif // CLIENT_H
                                            
                                            

                                            The main.cpp on the client side is,

                                            #include <QCoreApplication>
                                            #include "client.h"
                                            
                                            int main(int argc, char *argv[])
                                            {
                                                QCoreApplication a(argc, argv);
                                                Client client;
                                            
                                                return a.exec();
                                            }
                                            
                                            
                                            jsulmJ 1 Reply Last reply 17 May 2022, 05:38
                                            0

                                            1/22

                                            12 May 2022, 14:38

                                            • Login

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