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. QTcpSocket not Receiving Data Sent from Server
Forum Updated to NodeBB v4.3 + New Features

QTcpSocket not Receiving Data Sent from Server

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.1k 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.
  • D Offline
    D Offline
    deleted159
    wrote on last edited by
    #1

    I'm having trouble sending data from a server to a client using Qt. The client is not receiving the data I'm sending from the server, which is a test string of Some random data (I've tried with and without the newline character), but I'm not even sure if the data is being sent to the client, because after doing client->write(), I get an Unknown error when doing client->errorString(), and the bytesWritten signal is never emitted.

    The writeData method is what is being called in the MainWindow class, but to try and narrow down the cause of the problem, I moved the writing of data to the client into the newConnection method.

    The message Connection received is printed to the output window. I'm sending the string Some random data in the newConnection method to the client for testing purposes, but this is not being received by the client (the code to output the received data on the client side is inside the Character::readData() method).

    client->errorString() gives Unknown error, and then the message Bytes written is printed (even though, evidently, nothing is written, but I'm just using it as a status message).

    Server.cpp

    #include "Server.h"
    
    Server::Server(QObject *parent) : QObject(parent)
    {
        server = new QTcpServer(this);
    
        qDebug() << connect(server, SIGNAL(newConnection()), SLOT(newConnection()));
        qDebug() << connect(server, SIGNAL(bytesWritten()), SLOT(bytesWritten()));
    
        qDebug() << "Listening:" << server->listen(QHostAddress::Any, 1451);
    
        server->waitForNewConnection(-1);
    }
    
    void Server::newConnection()
    {
        qDebug("Connection received");
    
        client = server->nextPendingConnection();
    
        client->write("Some random data\n");
    
        qDebug() << "Error: " << client->errorString();
    
        qDebug() << "Bytes written";
    }
    
    void Server::bytesWritten(qint64 bytes)
    {
        qDebug() << "Bytes written: " << QString::number(bytes);
    }
    
    void Server::writeData(std::string data)
    {
        QByteArray byteArray = QByteArray(data.c_str());
    
        qDebug() << "Write data: " << QString::fromStdString(data);
        client->write(byteArray);
    }
    

    Client.cpp

    #include "Client.h"
    #include "mainwindow.h"
    
    Client::Client(QObject* parent) : QObject(parent)
    {
        socket = new QTcpSocket(this);
    
        (void)QObject::connect(socket, SIGNAL(connected()), this, SLOT(connected()));
        qDebug() << "Connect signal" << QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
    }
    
    bool Client::connectToHost(QString host)
    {
        socket->connectToHost(host, 1451);
        socket->waitForConnected();
    
        qDebug() << "Error: " << QString::number(socket->error() == QAbstractSocket::UnknownSocketError);
    
        return true;
    }
    
    void Client::connected()
    {
        qDebug("Socket is connected");
    
        qDebug() << QString::number(socket->state() == QAbstractSocket::ConnectedState);
    }
    
    void Client::readData()
    {
        qDebug("Read data");
    
        QTcpSocket* sender = static_cast<QTcpSocket*>(QObject::sender());
        QByteArray data = sender->readAll();
    
        std::string character = data.toStdString();
    
        qDebug() << "Character received: " << QString::fromStdString(character);
    
        MainWindow::characterReceived(character);
    }
    

    The weird thing is that the server says Connection received, but then says Error: Unknown error on my debug line (line 28 of Server.cpp). And the client is in the ConnectedState according to the debug line (line 26 of Client.cpp prints out 1, or true), but it also gives UnknownSocketError (line 17 of Client.cpp).

    JonBJ 1 Reply Last reply
    0
    • D deleted159

      I'm having trouble sending data from a server to a client using Qt. The client is not receiving the data I'm sending from the server, which is a test string of Some random data (I've tried with and without the newline character), but I'm not even sure if the data is being sent to the client, because after doing client->write(), I get an Unknown error when doing client->errorString(), and the bytesWritten signal is never emitted.

      The writeData method is what is being called in the MainWindow class, but to try and narrow down the cause of the problem, I moved the writing of data to the client into the newConnection method.

      The message Connection received is printed to the output window. I'm sending the string Some random data in the newConnection method to the client for testing purposes, but this is not being received by the client (the code to output the received data on the client side is inside the Character::readData() method).

      client->errorString() gives Unknown error, and then the message Bytes written is printed (even though, evidently, nothing is written, but I'm just using it as a status message).

      Server.cpp

      #include "Server.h"
      
      Server::Server(QObject *parent) : QObject(parent)
      {
          server = new QTcpServer(this);
      
          qDebug() << connect(server, SIGNAL(newConnection()), SLOT(newConnection()));
          qDebug() << connect(server, SIGNAL(bytesWritten()), SLOT(bytesWritten()));
      
          qDebug() << "Listening:" << server->listen(QHostAddress::Any, 1451);
      
          server->waitForNewConnection(-1);
      }
      
      void Server::newConnection()
      {
          qDebug("Connection received");
      
          client = server->nextPendingConnection();
      
          client->write("Some random data\n");
      
          qDebug() << "Error: " << client->errorString();
      
          qDebug() << "Bytes written";
      }
      
      void Server::bytesWritten(qint64 bytes)
      {
          qDebug() << "Bytes written: " << QString::number(bytes);
      }
      
      void Server::writeData(std::string data)
      {
          QByteArray byteArray = QByteArray(data.c_str());
      
          qDebug() << "Write data: " << QString::fromStdString(data);
          client->write(byteArray);
      }
      

      Client.cpp

      #include "Client.h"
      #include "mainwindow.h"
      
      Client::Client(QObject* parent) : QObject(parent)
      {
          socket = new QTcpSocket(this);
      
          (void)QObject::connect(socket, SIGNAL(connected()), this, SLOT(connected()));
          qDebug() << "Connect signal" << QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
      }
      
      bool Client::connectToHost(QString host)
      {
          socket->connectToHost(host, 1451);
          socket->waitForConnected();
      
          qDebug() << "Error: " << QString::number(socket->error() == QAbstractSocket::UnknownSocketError);
      
          return true;
      }
      
      void Client::connected()
      {
          qDebug("Socket is connected");
      
          qDebug() << QString::number(socket->state() == QAbstractSocket::ConnectedState);
      }
      
      void Client::readData()
      {
          qDebug("Read data");
      
          QTcpSocket* sender = static_cast<QTcpSocket*>(QObject::sender());
          QByteArray data = sender->readAll();
      
          std::string character = data.toStdString();
      
          qDebug() << "Character received: " << QString::fromStdString(character);
      
          MainWindow::characterReceived(character);
      }
      

      The weird thing is that the server says Connection received, but then says Error: Unknown error on my debug line (line 28 of Server.cpp). And the client is in the ConnectedState according to the debug line (line 26 of Client.cpp prints out 1, or true), but it also gives UnknownSocketError (line 17 of Client.cpp).

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Bob-Jones
      In a word, I think that you should not be looking at any socket->error() unless the operation returned that an error has occurred. Otherwise, you may just get "unknown error". I can't follow from what you say, but does the code actually work regardless of error message?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deleted159
        wrote on last edited by
        #3

        The code gets as far as receiving a connection from the client, and then it's supposed to send data to the client from the client->write(...) method, but I'm not sure if the problem is that the data is not being written, or if the client is just not receiving the data.

        In essence, the client is not receiving the data sent from the server, no matter what I try.

        Also, I thought that QTcpServer had a bytesWritten signal, but that was for QTcpSocket, so you can ignore that. I've remove that code, but that hasn't seemed to make a difference.

        Let me known if you need more details.

        Pablo J. RoginaP 1 Reply Last reply
        0
        • D deleted159

          The code gets as far as receiving a connection from the client, and then it's supposed to send data to the client from the client->write(...) method, but I'm not sure if the problem is that the data is not being written, or if the client is just not receiving the data.

          In essence, the client is not receiving the data sent from the server, no matter what I try.

          Also, I thought that QTcpServer had a bytesWritten signal, but that was for QTcpSocket, so you can ignore that. I've remove that code, but that hasn't seemed to make a difference.

          Let me known if you need more details.

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #4

          @Bob-Jones what if you try checking this example about how to setup a client and server in asynchronous way.

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. 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