PDA

View Full Version : QTcpSocket KeepAlive



antweb
16th April 2015, 08:20
void MyTcpSocket::doConnect()
{
socket = new QTcpSocket(this);
openrtspsetup = new QProcess(this);
connect(socket, SIGNAL(connected()),this, SLOT(connected()));
connect(socket, SIGNAL(disconnected()),this, SLOT(disconnected()));
connect(socket, SIGNAL(bytesWritten(qint64)),this, SLOT(bytesWritten(qint64)));
connect(socket, SIGNAL(readyRead()),this, SLOT(readyRead()));
qDebug() << "connecting..."; // this is not blocking call
socket->setSocketOption(QAbstractSocket::KeepAliveOption,t rue);
socket->connectToHost("192.168.0.128", 554); // we need to wait...
if(!socket->waitForConnected(5000))
{ qDebug() << "Error: " << socket->errorString(); }
}

void MyTcpSocket::connected()
{
qDebug() << "connected...";
socket->write("OPTIONS rtsp://192.168.0.128:554 RTSP/1.0");
}

void MyTcpSocket::disconnected()
{
qDebug() << "disconnected...";
}

void MyTcpSocket::bytesWritten(qint64 bytes)
{
qDebug() << bytes << " bytes written...";
}

void MyTcpSocket::readyRead()
{
qDebug() << "reading..."; // read the data from the socket
qDebug() << socket->readAll();
}

void MyTcpSocket::on_pushButton_clicked()
{

socket->write("DESCRIBE rtsp://192.168.0.128:554 RTSP/1.0");
}


As soon as I am connected the first write on the socket works with an OK reply from server but if I don't press the button then it automatically disconnects in about 8-10 seconds.
Even if I do press the button it gives me a bad request and disconnects.
Why is KeepAlive not working. Also what is wrong with my describe statement?

Thanks.

jefftee
16th April 2015, 09:02
I believe your DESCRIBE is failing because you did not identify a resource in the request



socket->write("DESCRIBE rtsp://192.168.0.128:554/example.mp4 RTSP/1.0");


Where example.mp4 is the resource you are asking to DESCRIBE, etc.

anda_skoa
16th April 2015, 10:03
Are you sure the other end is not the one disconncting you?

Cheers,
_

antweb
16th April 2015, 10:54
Yes,I'm pretty sure.
Because if in my button I put the OPTIONS command instead of DESCRIBE and keep pressing the button it keeps giving me server replies without disconnecting.
It's only when I stop sending any command then it gets disconnected. Is my KeepAlive statement correct?
Also I am not able to understand how to make my DESCRIBE command work. When i use OpenRTSP through my terminal it has implemented DESCRIBE in the same way.
I don't know whats wrong!

wysota
16th April 2015, 12:47
The lack of keep-alive option for TCP would not disconnect you after 5 or 10 seconds. Keep-alive packets are usually sent by the system after 2 hours of inactivity (and then every couple of minutes). At least those are the default times for my system (7200 and 75 seconds). So whatever is disconnecting you, it is not the lack of keep-alive for the socket.