PDA

View Full Version : Need Help with QTcpSocket



jknotzke
24th October 2009, 20:16
Hi,

I have a very simple client inspired from the fortuneclient. But for some reason, my readFortune never gets called. It connects, no errors but the slot is never called. It's as if it never gets any data.

Using nc I am able to see data streaming from the server. The host and port most definitely exist.

QuarqdClient is of type QObject...

Any ideas ?

Thanks

J


#include <QtNetwork>

#include "QuarqdClient.h"


QuarqdClient::QuarqdClient()
{
tcpSocket = new QTcpSocket(this);
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
requestNewFortune();
//readFortune();

}

void QuarqdClient::requestNewFortune()
{
blockSize = 0;
tcpSocket->abort();
tcpSocket->connectToHost("192.168.1.2", 8168);

}

void QuarqdClient::readFortune()
{

qDebug() << "Reading!!";
QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_0);

if (blockSize == 0) {
if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
return;
in >> blockSize;
}

if (tcpSocket->bytesAvailable() < blockSize)
return;

QString nextFortune;
in >> nextFortune;
qDebug() << in;

}

void QuarqdClient::displayError(QAbstractSocket::Socket Error socketError)
{
switch (socketError) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
qDebug() << "Host Not Found";
break;
case QAbstractSocket::ConnectionRefusedError:
qDebug() << "Connection Refused";
break;
default:
qDebug() << tcpSocket->errorString();
}

}

caduel
25th October 2009, 07:44
Have you made sure that the connect-calls really work (check the return codes)?

You might also want to monitor network traffic, or try telnet to check if connecting to that port produces any output (telnet 192.168.1.2 8168).

jknotzke
25th October 2009, 13:55
Ok I figured it out. I wasn't setting up my constructor correctly.

Thanks!

J