PDA

View Full Version : readyRead problem



fruzzo
25th August 2009, 10:43
I've a problem with my client/server application...the strange behaviour is the following:
1. Connect client-server
2. Client send a message (msg#1) to server (time To) (this kind of message is sended to server every 5 seconds).
2. Server receive msg#1 istantly (To+a bit latency delay) and send to client a new message (msg#2) just after msg# is received (time T1).
2. Normally client must receive the msg#2 istantly T1 instead it receive msg#2 after 5 seconds, after client send the second time the msg#1.

It seem that something is locking the readyRaed signal and only when I send the second time the msg#1 it's unlocked.

Any idea about this problem?

You can find the source code in this 3D:
http://www.qtcentre.org/forum/f-qt-programming-2/t-qtcpsocket-connectingstate-22885.html

wysota
25th August 2009, 11:23
Do you have the event loop running in the thread where you handle the connection?

fruzzo
25th August 2009, 14:29
Do you have the event loop running in the thread where you handle the connection?

yes...infact I intercept other signal of my TcpRadio, except the readyRead signal or better...I intercept it with a delay of exactly 5 sec. respect what I want. :confused:

wysota
25th August 2009, 16:33
Do you use any waitFor...() methods?

fruzzo
25th August 2009, 18:42
Do you use any waitFor...() methods?

yes...when I send a message:


bool TcpConnection::sendMessage(const RadioMessageInterface* msg)
{
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);

int msgSize = 0;
const char* data = msg->getData(msgSize);

out<<(quint16)msgSize;
out.writeRawData(data,msgSize);
out.device()->seek(0);
out << quint16(block.size() - sizeof(quint16));
int size = _socket->write(block);


if(size != block.size())
{
return false;
}

if(!_socket->waitForBytesWritten(_timeout))
return false;

return true;
}

wysota
25th August 2009, 21:49
yes...

Why? There is no point in doing that if you have an event loop running...

fruzzo
26th August 2009, 07:43
Why? There is no point in doing that if you have an event loop running...

mhmmm...strange because if I remove the waitForBytesWritten no messagegs are sended on the socket :eek:

wysota
26th August 2009, 07:52
Then you have no event loop running in your thread. Where do you call QThread::exec()?

fruzzo
27th August 2009, 08:28
Then you have no event loop running in your thread. Where do you call QThread::exec()?

This is the handler thread of the tcp radio:


//************************************************** ************************************************** ******************************************
//Class TcpRadio Handler
//************************************************** ************************************************** ******************************************

TcpRadioHandler::TcpRadioHandler(RADIO_MODE_TYPE mode, int localPort,const Node& remoteNode, RadioMessageInterfaceCreatorFunction messageCreator, int timeout, char * logDir)
:QThread(0), _tcpRadio(0), _mode(mode), _localPort(localPort), _peerNode(remoteNode), _creator(messageCreator), _timeout(timeout)
{
strcpy_s(_logDir, logDir);
start();
}

TcpRadioHandler::~TcpRadioHandler()
{
wait();
if (_tcpRadio)
delete _tcpRadio;
}

TcpRadio * TcpRadioHandler::radio()
{
return _tcpRadio;
}

void TcpRadioHandler::run()
{
_tcpRadio = new TcpRadio(_mode, _localPort, _peerNode, _creator, _timeout, _logDir);
exec();
}

wysota
27th August 2009, 08:50
Where and how do you create the socket you use in TcpConnection::sendMessage?

fruzzo
2nd September 2009, 18:35
Where and how do you create the socket you use in TcpConnection::sendMessage?


void TcpRadio::connectToPeer()
{
if (_mode == SERVER)
return;
_peerNode.setConnection(TcpConnection::openOutcomi ngConnection(_peerNode.address(), _peerNode.port(), _creator, _timeout));
const TcpConnection * conn = _peerNode.getConnection();
connect(conn, SIGNAL(status(QString)), this, SIGNAL(updateStatus(QString)));
connect(conn, SIGNAL(status(QString)), this, SLOT(writeLog(QString)));
connect(conn, SIGNAL(readyRead()), this, SLOT(receive()));
emit updateStatus("Opening connection with peer: " + _peerNode.address());
}

TcpConnection* TcpConnection::openOutcomingConnection(const QString& serverName, int port, RadioMessageInterfaceCreatorFunction messageCreator, int timeout)
{
QTcpSocket* socket = new QTcpSocket();
socket->connectToHost(serverName, port, QIODevice::ReadWrite);
return new TcpConnection(socket, messageCreator, timeout);
}


Howere I've solved the problem...without using signal/slots mecanism! :mad:

Now I've another problem with disconnection...when I try to use disconnectFromHost from server, It don't disconnect (socket state is still connected) and the socket return a "Network time out" error. Than the Client crash on waitReadyRead.
I try to insert a waitForDisconnected() just after the call of disconnectFromHost but without results: waitForDisconnected return always false ( waiting 30000 msecs too).

Why? :confused:

wysota
2nd September 2009, 19:50
You should really make up your mind if you want to use the event queue or not.