TcpSocket signal readyRead not received
-
I'm having the issue mentioned inside the subject when building an tcp client for a server that is created with socat. Is there someone that had the same issue ?
#redirect the stdio on tcp port 2728
socat - tcp-listen:2728,reuseaddr,forkOn the same machine, I'm running the code that following(more or less, because I'm changing the code at each 5 minutes in case I have a tip):
CSocatMonitor::CSocatMonitor(QObject *parent) : QDialog(parent) { qDebug() << "The fun is starting..."; } void CSocatMonitor::init() { socket = new QTcpSocket(this); in.setDevice(socket); in.setVersion(QDataStream::Qt_5_9); connect(socket, SIGNAL(readyRead()),this, SLOT(onReadyRead())); connect(socket, &QAbstractSocket::connected,this, &CSocatMonitor::connected); connect(socket, &QAbstractSocket::disconnected,this, &CSocatMonitor::disconnected); connect(socket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), this, &CSocatMonitor::getError); socket->connectToHost("127.0.0.1",2728); } void CSocatMonitor::onReadyRead() { in.startTransaction(); QString nextFortune; in >> nextFortune; if (!in.commitTransaction()) return; QMessageBox::information(this, tr("Fortune Client"), tr("The following data occurred: %1.") .arg(nextFortune)); socket->flush(); } void CSocatMonitor::connected() { qDebug() << "connected..."; QMessageBox::information(this, tr("connected..."), tr("connected..." "connected...")); QMessageBox::information(this, tr("Fortune Client"), tr("The following data(remaining length %1) occurred: %2.") .arg(QString(socket->readAll())) .arg(socket->readAll().length())); socket->flush(); } void CSocatMonitor::disconnected() { QMessageBox::information(this, tr("DISconnected..."), tr("DISconnected..." "DISconnected...")); } void CSocatMonitor::getError(QAbstractSocket::SocketError socketError) { qDebug() << "Socket error:" << socketError; QMessageBox::information(this, tr("Fortune Client"), tr("The following error occurred: %1.") .arg(socket->errorString())); } -
Did you declare
onReadyReadas slot?
Otherwise, to be on the safe side, changeconnect(socket, SIGNAL(readyRead()),this, SLOT(onReadyRead()));toconnect(socket, &QTcpSocket::readyRead,this, &CSocatMonitor::onReadyRead); -
@Catalin.O said in TcpSocket signal readyRead not received:
Just typing into the stdio. This output is provided by socat over the TCP.
Can you post code?
-
@Catalin.O said in TcpSocket signal readyRead not received:
Just typing into the stdio. This output is provided by socat over the TCP.
Can you post code?
-
@Catalin.O
Yes, it would be better. -
I'm having the issue mentioned inside the subject when building an tcp client for a server that is created with socat. Is there someone that had the same issue ?
#redirect the stdio on tcp port 2728
socat - tcp-listen:2728,reuseaddr,forkOn the same machine, I'm running the code that following(more or less, because I'm changing the code at each 5 minutes in case I have a tip):
CSocatMonitor::CSocatMonitor(QObject *parent) : QDialog(parent) { qDebug() << "The fun is starting..."; } void CSocatMonitor::init() { socket = new QTcpSocket(this); in.setDevice(socket); in.setVersion(QDataStream::Qt_5_9); connect(socket, SIGNAL(readyRead()),this, SLOT(onReadyRead())); connect(socket, &QAbstractSocket::connected,this, &CSocatMonitor::connected); connect(socket, &QAbstractSocket::disconnected,this, &CSocatMonitor::disconnected); connect(socket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error), this, &CSocatMonitor::getError); socket->connectToHost("127.0.0.1",2728); } void CSocatMonitor::onReadyRead() { in.startTransaction(); QString nextFortune; in >> nextFortune; if (!in.commitTransaction()) return; QMessageBox::information(this, tr("Fortune Client"), tr("The following data occurred: %1.") .arg(nextFortune)); socket->flush(); } void CSocatMonitor::connected() { qDebug() << "connected..."; QMessageBox::information(this, tr("connected..."), tr("connected..." "connected...")); QMessageBox::information(this, tr("Fortune Client"), tr("The following data(remaining length %1) occurred: %2.") .arg(QString(socket->readAll())) .arg(socket->readAll().length())); socket->flush(); } void CSocatMonitor::disconnected() { QMessageBox::information(this, tr("DISconnected..."), tr("DISconnected..." "DISconnected...")); } void CSocatMonitor::getError(QAbstractSocket::SocketError socketError) { qDebug() << "Socket error:" << socketError; QMessageBox::information(this, tr("Fortune Client"), tr("The following error occurred: %1.") .arg(socket->errorString())); }@Catalin.O said in TcpSocket signal readyRead not received:
in.startTransaction();
QString nextFortune; in >> nextFortune; if (!in.commitTransaction()) return;Unless your server knows what Qt is and how it should send you
QStrings throughQDataStreamversion 5.9, then this should always fail and you'd get nowhere with your slot. -
@Catalin.O
Yes, it would be better. -
@Catalin.O said in TcpSocket signal readyRead not received:
in.startTransaction();
QString nextFortune; in >> nextFortune; if (!in.commitTransaction()) return;Unless your server knows what Qt is and how it should send you
QStrings throughQDataStreamversion 5.9, then this should always fail and you'd get nowhere with your slot.@kshegunov Your suggestion solved my blocking point. Thank you.