[Interest] QTcpSocket on error Signal not emitted
Abhishek
abhishekworld at gmail.com
Fri May 3 10:25:57 CEST 2013
Hello,
I am writing a Tcp client program, which ping server every 3 sec.
the general behaviour is :
1) client connects to server
2) send a identification packet
3) server checks the packet and reply accordingly
*4) server ends the connection. (not client) *
so I get a error QAbstractSocket::error(QAbstractSocket::SocketError)
= RemoteHostClosedError
(I guess its because server ends the connection)
but when I get this error the QAbstractSocket::disconnected() signal is
emited
but this is not emitted for other types of error which is confusing me.
*
*
*and some times its stop pinging !*
*
*
following is my client code :
*// H*
#ifndef PING_H
#define PING_H
#include <QObject>
#include <QtNetwork/QTcpSocket>
struct PingData;
class Ping : public QObject
{
Q_OBJECT
public:
explicit Ping(const QString& address, qint16 port, QObject *parent = 0);
void start();
private slots:
void connectedToserver();
void readFromserver();
void disconnectedFromServer();
void timerInterval();
void clientError(QAbstractSocket::SocketError error);
private:
PingData* d;
};
#endif // PING_H
*// CPP*
#include "Ping.h"
#include <QTimer>
#include <QDebug>
#define PINGTIME 3000
struct PingData {
QString address;
qint16 port;
QTcpSocket* socket;
};
Ping::Ping(const QString& address, qint16 port, QObject *parent) :
QObject(parent)
{
d = new PingData;
d->address = address;
d->port = port;
d->socket = new QTcpSocket(this);
connect(d->socket, SIGNAL(connected()), this, SLOT(connectedToserver()));
connect(d->socket, SIGNAL(readyRead()), this, SLOT(readFromserver()));
connect(d->socket, SIGNAL(disconnected()), this,
SLOT(disconnectedFromServer()));
connect(d->socket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(clientError(QAbstractSocket::SocketError)));
}
void Ping::connectedToserver()
{
QString packet = "[nomad:121212,]\r\nEOF\r\n";
d->socket->write(packet.toAscii());
}
void Ping::readFromserver()
{
qDebug() << d->socket->readAll();
}
void Ping::disconnectedFromServer()
{
d->socket->reset();
if(d->socket->isOpen())
d->socket->close();
QTimer::singleShot(PINGTIME, this, SLOT(timerInterval()));
}
void Ping::timerInterval()
{
if(d->socket->isOpen())
d->socket->close();
d->socket->connectToHost(d->address, d->port);
}
void Ping::start()
{
QTimer::singleShot(PINGTIME, this, SLOT(timerInterval()));
}
void Ping::clientError(QAbstractSocket::SocketError error)
{
qDebug() << Q_FUNC_INFO << " : " << (int)error;
if(error == QAbstractSocket::RemoteHostClosedError)
{
d->socket->disconnectFromHost();
// emits the signal disconnected
}
else
{
d->socket->close();
this->disconnectedFromServer();
// else call the slot directly
}
}
Please let me know where I am going wrong.
Thanks
--
Abhishek
http://thezeroth.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20130503/fb041744/attachment.html>
More information about the Interest
mailing list