PDA

View Full Version : [SOLVED] QTcpSocket signals problem



RafaelRSE
8th October 2014, 15:11
Hi,

I'm having trouble with QTcpSocket signals. I'm using QTcpSocket::write() to request the server some data and trying to use the signal readyRead() to receive those data, but it doesn't work. I tried to using others signals (just for test) and none of those work.

Here is my code:

In the constructor I have:


buffer = new QBuffer(this);
socket = new QTcpSocket(this);
buffer->open(QIODevice::ReadWrite);

connect(socket, SIGNAL(readyRead()), SLOT(receiveMessage()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
SLOT(mostrarErro(QAbstractSocket::SocketError)));

The function to connect to the host:


void Client::connectToServer()
{
Settings::ConnectionData p = c->data(); //ConnectionData is a struct to receive the Data from other form and c is the other form class object

socket = p.socket;
port = p.port;
buffer = p.buffer;

if (socket->state() == QAbstractSocket::UnconnectedState) {
socket->connectToHost(setIp(), port);
}
else {
socket->disconnectFromHost();
}
}


I'm sure that the function receiveMessage() is not being called.

yeye_olive
8th October 2014, 15:51
In the constructor you allocate a QTcpSocket and connect its readyRead() signal to your receiveMessage() slot. In conectToServer() (sic) you may be connecting another socket (a pointer to which you got in p.socket) to a server.

RafaelRSE
8th October 2014, 15:55
In the constructor you allocate a QTcpSocket and connect its readyRead() signal to your receiveMessage() slot. In conectToServer() (sic) you may be connecting another socket (a pointer to which you got in p.socket) to a server.

Yes, I just realized that and I corrected it.

Thank you!