Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Tcp/Ip client-server communication in qt c++



  • Hallo

    i have created tcp/ip client server communication.i can send messages from client to server. but i have a issue.. i have to display the sent message as binary on server.i am unable to do this. help will be appreciated.

    @client
    #include "client.h"
    #include <QHostAddress>

    Client::Client(QObject* parent): QObject(parent)
    {
    connect(this, SIGNAL(startTransferSignal(QString)), this,
    SLOT(startTransfer(QString)));
    connect(this, SIGNAL(startSignal(QString,quint16)), this,
    SLOT(start(QString, quint16)));
    connect(this, SIGNAL(Disconnect()), this, SLOT(DisconnectSlot()));
    }

    void Client::DisconnectSlot()
    {
    client.close();
    }

    Client::~Client()
    {
    client.close();
    }

    void Client::start(QString address, quint16 port)
    {
    QHostAddress addr(address);
    client.connectToHost(addr, port);
    }

    void Client::startTransfer(QString msg)
    {
    quint32 length = msg.length();
    client.write((char*)&length, sizeof(length));
    client.write(msg.toUtf8(), msg.length());
    }

    @server
    #include "server.h"
    #include <iostream>
    using namespace std;

    Server::Server(QObject* parent): QObject(parent)
    {
    connect(&server, SIGNAL(newConnection()),
    this, SLOT(acceptConnection()));
    }

    void Server::start()
    {
    server.listen(QHostAddress::Any, 8080);
    }

    Server::~Server()
    {
    server.close();
    }

    void Server::acceptConnection()
    {
    client = server.nextPendingConnection();
    qDebug() << "Connected to client";

    connect(client, SIGNAL(readyRead()),
    this, SLOT(startRead()));
    }

    void Server::startRead()
    {
    while (1)
    {
    char buffer[1024] = {0};
    quint32 length = 0;

      if (client->bytesAvailable() < 4)
      {
          if (!client->waitForReadyRead(-1))
              break;
      }
    
      client->read((char*)&length, sizeof(length));
    
      while (client->bytesAvailable() < length)
          client->waitForReadyRead(-1);
    
      quint32 read = 0;
      do
      {
        read += client->read(buffer, length);
        qDebug() << "Message received from client:" << QString::fromLatin1(buffer) << endl;
    
      }while (read < length);
    

    }

    client->close();
    qDebug() << "Disconnected from client";
    }


  • Moderators

    @karthik23
    i don't know if i got you correct, but is this what you are looking for?

    QByteArray(buffer, length).toHex();
    


  • @raven-worx

    i exactly dont know. i am new to Qt. if you can, kindly help me to figure it out.
    Thank you!


  • Moderators

    @karthik23
    i neither don't know what you want to achieve exactly.
    The example i give displays the received binary data as hex values. Is that already what you want?



  • @raven-worx
    yeah..may be similar one. where should i insert this code exactly?

    Thanks!


  • Moderators

    @karthik23 said:

    where should i insert this code exactly?

    where you need it :)
    I mean u asked the question because you want to achieve something.
    Since it's still not very clear what you want as the final result, i can't help you.

    For testing you can add it in the line where your qDebug() calls is.