QTcpSocket doesn't emit the signal ReadyRead()
-
Hello,
Few days ago I have a trouble implementing a MultiClient Server, but finally I could solve my trouble (here is the link for the previous thread:https://forum.qt.io/topic/101298/qsocketnotifier-socket-notifiers-cannot-be-enabled-or-disabled-from-another-thread/23)
However, since I found the solution, I have been having other trouble: My goal is implenting a code for a Multiclient server that can read the messages from a device (A Pixhawk, is a device that is continously sending messages in form of package). With these messages group in form of QByteArray, I send this QbyteArray to my clients (programs that can decode the message and extract the information that the Pixhawk is trying to transmit). However I need to implement otrher function: i need to send a message from my client to the Pixhawk.
I made a client code that I know it can send and receive messages from a server (I prove it with a server code that I made and just can handle one connection and it also works with a multiclient server with the error that I had), so I think the trouble is in the solution that I found for my Server code.
This is my code (it's a bit different that the code from my previous thread):server.cpp
#include "myserver.h" MyServer::MyServer(QObject *parent) : QTcpServer (parent) { } void MyServer::startServer() { if(!this->listen(QHostAddress::Any, 9999)){ qDebug()<<"Server not started"; }else { qDebug()<<"Server listening"; } } void MyServer::startSerialPort() { mipuerto2 = new MySerialPort; connect(mipuerto2, SIGNAL(msgChanged(QByteArray*)), this, SLOT(getMens(QByteArray*))); mipuerto2->openSerialPort(); } void MyServer::getMens(QByteArray*array) { emit mensChanged(array); } void MyServer::sendMens(QByteArray *arraySend) { mipuerto2->writeMsg(*arraySend); } void MyServer::incomingConnection(int socketDescriptor) { qDebug()<<socketDescriptor<<"Connecting... "; socket = new MySocket(socketDescriptor); QThread *thread = new QThread; qDebug()<<thread->currentThreadId(); connect(this, SIGNAL(mensChanged(QByteArray*)), socket, SLOT(getMsg(QByteArray*))); connect(socket, SIGNAL(mensajeEnviarSocket(QByteArray*)), this, SLOT(sendMens(QByteArray*))); connect(socket, SIGNAL(disconnected()),thread, SLOT(quit()) ); connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater())); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); connect(thread, SIGNAL(started()), socket, SLOT(funcionamiento())); socket->moveToThread(thread); thread->start(); }
mysocket.cpp
#include "mysocket.h" MySocket::MySocket(int socketDescriptor) { ID=socketDescriptor; } void MySocket::leerMsg() { qDebug()<<"Mensaje entrante"; msgSend=""; socket->waitForReadyRead(100); msgSend=socket->readAll(); qDebug()<<msgSend.toHex(); emit mensajeEnviarSocket(&msgSend); } void MySocket::funcionamiento() { socket= new QTcpSocket; estado=socket->setSocketDescriptor(ID, QAbstractSocket::ConnectedState, QIODevice::ReadWrite); connect(this, SIGNAL(disconnected()), this, SLOT(desconexion())); connect(this, SIGNAL(readyRead()), this, SLOT(leerMsg())); qDebug()<<"Hola"; } void MySocket::desconexion() { socket->close(); socket->deleteLater(); } void MySocket::getMsg(QByteArray *array) { if(socket->waitForConnected(1000)){ arr=*array; socket->write(arr); socket->flush(); socket->waitForBytesWritten(3000); } }
I know that the troble is that the signal ReadyRead() is not emitted when I receive a new package of data from the client. As I said, I know that the client send the messages propertly; so I think that the trouble is with the class MySocket that don't work propertly. I have been the last hourse trying to find a solution.
-
@Dooham said in QTcpSocket doesn't emit the signal ReadyRead():
void MySocket::funcionamiento()
{
socket= new QTcpSocket;
estado=socket->setSocketDescriptor(ID, QAbstractSocket::ConnectedState, QIODevice::ReadWrite);
connect(this, SIGNAL(disconnected()), this, SLOT(desconexion()));
connect(this, SIGNAL(readyRead()), this, SLOT(leerMsg()));
qDebug()<<"Hola";
}Your problem seems to be there.
The signal is not connected to its origin "socket", but the object hosting "socket". Probably you need to change to
void MySocket::funcionamiento() { socket= new QTcpSocket; estado=socket->setSocketDescriptor(ID, QAbstractSocket::ConnectedState, QIODevice::ReadWrite); connect(socket, SIGNAL(disconnected()), this, SLOT(desconexion())); connect(socket, SIGNAL(readyRead()), this, SLOT(leerMsg())); qDebug()<<"Hola"; }
You should check also the application's output (e.g. in pane Application Output of creator). There should be a message popping up that the connect failed.
Alternatively you can use the functor syntax for connect which is giving you an error message at building time.
void MySocket::funcionamiento() { socket= new QTcpSocket; estado=socket->setSocketDescriptor(ID, QAbstractSocket::ConnectedState, QIODevice::ReadWrite); connect(socket, &QTcpSocket::disconnected, this, &MySocket::desconexion); connect(socket, &QTcpSocket::readyRead, this, &MySocket::leerMsg); qDebug()<<"Hola"; }
When you try the latter syntax with "this" instead of socket you will see immediately the effect e.g.
connect(this, &QTcpSocket::disconnected, this, &MySocket::desconexion); connect(this, &QTcpSocket::readyRead, this, &MySocket::leerMsg);