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. Help needed in server and client communication !!
Qt 6.11 is out! See what's new in the release blog

Help needed in server and client communication !!

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 3.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello everyone, I am facing a wierd problem in server and client communication. When I send any data from server to client, from server side its showing data is sent but on client side no data appears to come and vice versa. Can anyone of you help me with the above mentioned problem please.

    Code and outputs are shown below:

    serverapp.cpp
    @
    #include "serverapp.h"
    #include "ui_serverapp.h"

    #include <stdlib.h>
    #include <QtNetwork>
    #include <QMessageBox>
    #include <QString>

    #include <QtWidgets>
    ServerApp::ServerApp(QWidget *parent):QMainWindow(parent),Ui::ServerApp()
    {
    setupUi(this);
    quint16 portnumber = 4444;
    statusLabel->setWordWrap(true);
    quitButton->setAutoDefault(false);
    if (!server.listen(QHostAddress::Any,portnumber))
    {
    QMessageBox::critical(this, tr("Server v1.0"),tr("Unable to start the server: %1.").arg(server.errorString()));
    close();
    return;
    }
    QString ipAddress;
    ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
    statusLabel->setText(tr("The server is running on IP: %1 port: %2\n").arg(ipAddress).arg(server.serverPort()));
    connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
    setWindowTitle(tr("Server v1.0"));
    }

    ServerApp::~ServerApp()
    {
    }

    void ServerApp::updateRB(QString data)
    {
    receivingBrowser->append(data.toUtf8());
    }

    FortuneServer::FortuneServer(QObject *parent):QTcpServer(parent)
    {

    }

    void FortuneServer::incomingConnection(qintptr socketDescriptor)
    {
    FortuneThread *thread = new FortuneThread(socketDescriptor, this);
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    thread->start();
    }

    FortuneThread::FortuneThread(int socketDescriptor, QObject *parent): QThread(parent), socketDescriptor(socketDescriptor)
    {

    }

    void FortuneThread::run()
    {
    QTcpSocket tcpSocket;
    if (!tcpSocket.setSocketDescriptor(socketDescriptor))
    {
    emit error(tcpSocket.error());
    return;
    }
    else
    {
    qDebug() << "Connected to client";
    qDebug() << tcpSocket.readAll();//tmp.data();
    tcpSocket.write("Hello client");
    tcpSocket.disconnectFromHost();
    }
    }
    @

    serverapp.h
    @
    #ifndef SERVERAPP_H
    #define SERVERAPP_H
    #include "ui_serverapp.h"
    #include <QMainWindow>
    #include <QTcpServer>
    #include <QThread>
    #include <QTcpSocket>

    #include <QMessageBox>

    namespace Ui {
    class ServerApp;
    }

    class FortuneServer : public QTcpServer
    {
    Q_OBJECT

    public:
    FortuneServer(QObject *parent = 0);

    protected:
    void incomingConnection(qintptr socketDescriptor);

    private:
    };

    class FortuneThread : public QThread
    {
    Q_OBJECT

    public:

    FortuneThread(int socketDescriptor, QObject *parent);
    void run();
    

    signals:
    void error(QTcpSocket::SocketError socketError);
    void displayRB(QString);

    private:
    int socketDescriptor;
    };

    class ServerApp : public QMainWindow, public Ui::ServerApp
    {
    Q_OBJECT

    public:
    ServerApp(QWidget *parent = 0);
    ~ServerApp();

    private slots:
    void updateRB(QString);

    private:
    FortuneServer server;
    };

    #endif // SERVERAPP_H
    @

    rescueop.cpp
    @
    #include "rescueop.h"
    #include "ui_rescueop.h"
    #include <QTcpSocket>

    QTcpSocket socket;

    rescueOp::rescueOp(QWidget *parent) : QMainWindow(parent),Ui::rescueOp()
    {
    qint16 i;
    setupUi(this);

    socket.connectToHost("127.0.0.1",4444);
    if(socket.waitForConnected(500))
    {
        status->setText("Connected to server");
        qDebug() << "Connected to server";
        for(i=0;i<5;i++)
        {
            qDebug() << socket.write("Hello server");
            serverBrowser->append("Hello server ");
        }
        socket.waitForBytesWritten();
        socket.readyRead();
        qDebug() << socket.readAll();
        socket.close();
    }
    else
        status->setText("Socket not connected");
    

    }

    rescueOp::~rescueOp()
    {
    if(socket.isOpen())
    socket.close();
    }
    @

    rescueop.h
    @
    #ifndef RESCUEOP_H
    #define RESCUEOP_H

    #include <QMainWindow>
    #include <QTcpSocket>
    #include "ui_rescueop.h"

    namespace Ui {
    class rescueOp;
    }

    class rescueOp : public QMainWindow, public Ui::rescueOp
    {
    Q_OBJECT

    public:
    rescueOp(QWidget *parent = 0);
    ~rescueOp();

    signals:

    private:
    };

    #endif // RESCUEOP_H
    @

    output of server
    @
    Starting /home/chocky/Desktop/mtech_project/build-ServerApp-Desktop_Qt_5_3_GCC_64bit-Debug/ServerApp...
    Connected to client
    ""
    /home/chocky/Desktop/mtech_project/build-ServerApp-Desktop_Qt_5_3_GCC_64bit-Debug/ServerApp exited with code 0
    @

    output of client
    @
    Starting /home/chocky/Desktop/mtech_project/build-rescue_original-Desktop_Qt_5_3_GCC_64bit-Debug/rescueOp...
    Connected to server
    12
    12
    12
    12
    12
    ""
    /home/chocky/Desktop/mtech_project/build-rescue_original-Desktop_Qt_5_3_GCC_64bit-Debug/rescueOp exited with code 0
    @

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

      Did you modify Fortune example?
      If yes, did you test original Fortune example?
      Does the Fortune example work?

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Original fortune example works fine. But I couldn't make this code work. Everything goes well except the data transmission. Can you help me with this code.?

        1 Reply Last reply
        0
        • V Offline
          V Offline
          Vincent007
          wrote on last edited by
          #4

          You can compare original Fortune example and your code and then modify original Fortune example to become your code gradually.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            Even I tried that previously in every possible way for transmitting data. Can you provide any working code for the communication between server and client if possible.

            1 Reply Last reply
            0
            • V Offline
              V Offline
              Vincent007
              wrote on last edited by
              #6

              original Fortune example shows how to perform communication between server and client.

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                I have got it solved by using readyRead signal, but now i got a new problem. Previously i used readyRead signal for serial port now that conflicts with the socket too. Do you know how can i use two readyRead signals for different purposes like the code shown below.?

                @
                connect(&serialPort,SIGNAL(QSerialPort::readyRead()),this,SLOT(serialReceived()));
                connect(&socket, SIGNAL(QTcpSocket::readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);

                void rescueOp::serialReceived()
                {
                // get the information
                QByteArray data = serialPort.readAll();

                // will write on console
                qDebug() << " Data in Serial Port: " << data;
                

                }

                void rescueOp::readyRead()
                {
                // get the information
                QByteArray Data = socket.readAll();

                // will write on console
                qDebug() << " Data in Socket: " << Data;
                

                }
                @

                It shows below error now,
                @
                Connected to server
                QObject::connect: No such signal QTcpSocket::QTcpSocket::readyRead() in ../rescue_original/rescueop.cpp:61
                QObject::connect: (receiver name: 'rescueOp')
                Serial Port Opened
                @

                1 Reply Last reply
                0
                • V Offline
                  V Offline
                  Vincent007
                  wrote on last edited by
                  #8

                  try Qt5 syntax connection,

                  @
                  Counter a, b;
                  QObject::connect(&a, &Counter::valueChanged,
                  &b, &Counter::setValue);
                  @

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi,

                    You are mixing the old and new connection syntax

                    @connect(&socket, SIGNAL(readyRead()), this, SLOT(readyRead()));@

                    is the correct standard syntax

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0

                    • Login

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