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. Tcp/Ip client-server communication in qt c++

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
tcp servertcpsockettcp
6 Posts 2 Posters 4.7k 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.
  • karthik23K Offline
    karthik23K Offline
    karthik23
    wrote on last edited by
    #1

    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";
    }

    raven-worxR 1 Reply Last reply
    0
    • karthik23K karthik23

      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";
      }

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

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

      QByteArray(buffer, length).toHex();
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • karthik23K Offline
        karthik23K Offline
        karthik23
        wrote on last edited by
        #3

        @raven-worx

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

        raven-worxR 1 Reply Last reply
        0
        • karthik23K karthik23

          @raven-worx

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

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @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?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • karthik23K Offline
            karthik23K Offline
            karthik23
            wrote on last edited by
            #5

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

            Thanks!

            raven-worxR 1 Reply Last reply
            0
            • karthik23K karthik23

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

              Thanks!

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @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.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              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