QTcpSocket not Receiving Data Sent from Server
-
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 doingclient->write()
, I get anUnknown error
when doingclient->errorString()
, and thebytesWritten
signal is never emitted.The
writeData
method is what is being called in theMainWindow
class, but to try and narrow down the cause of the problem, I moved the writing of data to the client into thenewConnection
method.The message
Connection received
is printed to the output window. I'm sending the stringSome random data
in thenewConnection
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 theCharacter::readData()
method).client->errorString()
gives Unknown error, and then the messageBytes 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 saysError: Unknown error
on my debug line (line 28 ofServer.cpp
). And the client is in theConnectedState
according to the debug line (line 26 ofClient.cpp
prints out1
, ortrue
), but it also givesUnknownSocketError
(line 17 ofClient.cpp
). -
@Bob-Jones
In a word, I think that you should not be looking at anysocket->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? -
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 abytesWritten
signal, but that was forQTcpSocket
, 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.
-
@Bob-Jones what if you try checking this example about how to setup a client and server in asynchronous way.