[Development] QTcpSocket disconnect signal surprise

Oleg Shalnev oleg.shalnev at gmail.com
Sun Dec 11 13:16:11 CET 2016


Good day, all in the list!


Some problems during detection "connection lost" via KeepAlive and
disconnect signal.



After enabling KeepAlive, if there is some data in the socket buffer,
disconnect signal is not working. So there is no way to detect cable lost.

After disconnecting, socket don't send disconnect signal, don't send
stateChanged signal, don't send error code.
bytesToWrite() returns 0 bytes.

If there is no data in the socket disconnected signal is working normal.

Here is the test code.

SocketTransport::SocketTransport(QObject *parent) : QObject(parent)
{
	QObject::connect(&mSocket, &QTcpSocket::disconnected, this,
&SocketTransport::onDisconnect);
	QObject::connect(&mSocket, &QTcpSocket::readyRead,   this,
&SocketTransport::readData);
	QObject::connect(&mSocket, &QTcpSocket::connected,   this,
&SocketTransport::onConnected);
	QObject::connect(&mTimer, &QTimer::timeout, this,
&SocketTransport::writeData, Qt::QueuedConnection);
}

void SocketTransport::establishConnection()
{
	mSocket.connectToHost("192.168.1.2", 3333);
}

void SocketTransport::onDisconnect()
{
	qDebug()<<"On disconnect";
}

void SocketTransport::readData()
{

}

void SocketTransport::onConnected()
{
	qDebug()<<"OnConnected";

	mSocket.setSocketOption(QAbstractSocket::KeepAliveOption, 1);

	int Idle=1;
	int Count=2;
	int Interval=1;

	if(setsockopt(static_cast<int>(mSocket.socketDescriptor()),
IPPROTO_TCP, TCP_KEEPIDLE,
	              &Idle, sizeof(Idle))<0)
		qDebug()<<"Error setsockopt IPPROTO_TCP, TCP_KEEPIDLE";

	if(setsockopt(static_cast<int>(mSocket.socketDescriptor()),IPPROTO_TCP,
TCP_KEEPCNT,
	              &Count, sizeof(Count))<0)
		qDebug()<<"Error setsockopt IPPROTO_TCP, TCP_KEEPCNT";

	if(setsockopt(static_cast<int>(mSocket.socketDescriptor()),
IPPROTO_TCP, TCP_KEEPINTVL,
	              &Interval, sizeof(Interval))<0)
		qDebug()<<"Error setsockopt IPPROTO_TCP, TCP_KEEPINTVL";

	mTimer.start(1000);
}

void SocketTransport::writeData()
{
	qDebug()<<"BYTES to write"<<mSocket.bytesToWrite()<<mSocket.state();

	if(mSocket.state()==QAbstractSocket::UnconnectedState)
	{
		qDebug()<<"Unconnected state";
		QCoreApplication::processEvents();
		return;
	}

	QUuid      id=QUuid::createUuid();
	QByteArray data=id.toByteArray();

	qDebug()<<id<<data.size();

	mSocket.write(data, data.size());
}


Can enybody help me with this problem?

Thank's a lot!!!

-- 
Oleg Shalnev  (Kalpa Knowledge Integration Initiative)
----------------------------------------------
mailto: oleg at kalpa.ru , oleg.shalnev at gmail.com
skype:  oleg_shalnev
cell    :  +7 (918) 741 7217
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/development/attachments/20161211/18051498/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Socket.tar.bz2
Type: application/x-bzip2
Size: 6641 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/development/attachments/20161211/18051498/attachment.bin>


More information about the Development mailing list