Juba
28th April 2011, 16:43
Hi all,
I have a trouble with QTcpSocket readyRead() signal.
My Qt application act as client and the server send messages in an asyncronous way.
The problem is that I notice that sometimes a message isn't signaled by the readyRead().
The message is always the same message in the sequence, but the problem don't always occours.
I'm sure that message comes because I see the network traffic with wireshark.
Here is my client code:
{
// in class ctor...
m_socket = new QTcpSocket(this);
connect(m_socket, SIGNAL(connected()), this,SLOT(connected()));
connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this,SLOT(error(QAbstractSocket::SocketError)));
connect(m_socket, SIGNAL(disconnected()), this,SLOT(disconnected()));
connect(m_socket, SIGNAL(readyRead()), this,SLOT(receiveMessage()));
}
void XmlSocket::receiveMessage()
{
int bytesRead;
// READ_BUF_SIZE is defined as 512 and the message I receive are 100 chars...
char buf[READ_BUF_SIZE];
// read data from socket
// NB: loop only if we read more than 512 bytes...
QString msgString;
do {
bzero(buf,READ_BUF_SIZE);
bytesRead = m_socket->read(buf, READ_BUF_SIZE);
if(bytesRead < 0)
qCritical("Error reading data from socket %d",bytesRead);
else if(bytesRead == 0)
qWarning("No more data on socket...");
msgString += QString(buf);
qDebug("++ %s", qPrintable(msgString));
} while(bytesRead == READ_BUF_SIZE);
// emit signal with available data
QDomDocument msg;
if(!msg.setContent(msgString))
qWarning("Error setting content for string %s", qPrintable(msgString));
emit messageReceived(msg);
}
I have see that the message that I don't receive is sent just 3 ms after the previous one...
Send time
[28/04 16:33:30:423] message 1, received
[28/04 16:33:30:525] message 2, received
[28/04 16:33:30:528] message 3, NOT RECEIVED
[28/04 16:33:31:290] message 4, received
This could be a performance issue???
Now I try to solve using standard socket, but I wish to use QTcpSocket because they offer a simple API and for portability reason.
I am using Qt 4.7.1 on linux platform.
I have a trouble with QTcpSocket readyRead() signal.
My Qt application act as client and the server send messages in an asyncronous way.
The problem is that I notice that sometimes a message isn't signaled by the readyRead().
The message is always the same message in the sequence, but the problem don't always occours.
I'm sure that message comes because I see the network traffic with wireshark.
Here is my client code:
{
// in class ctor...
m_socket = new QTcpSocket(this);
connect(m_socket, SIGNAL(connected()), this,SLOT(connected()));
connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this,SLOT(error(QAbstractSocket::SocketError)));
connect(m_socket, SIGNAL(disconnected()), this,SLOT(disconnected()));
connect(m_socket, SIGNAL(readyRead()), this,SLOT(receiveMessage()));
}
void XmlSocket::receiveMessage()
{
int bytesRead;
// READ_BUF_SIZE is defined as 512 and the message I receive are 100 chars...
char buf[READ_BUF_SIZE];
// read data from socket
// NB: loop only if we read more than 512 bytes...
QString msgString;
do {
bzero(buf,READ_BUF_SIZE);
bytesRead = m_socket->read(buf, READ_BUF_SIZE);
if(bytesRead < 0)
qCritical("Error reading data from socket %d",bytesRead);
else if(bytesRead == 0)
qWarning("No more data on socket...");
msgString += QString(buf);
qDebug("++ %s", qPrintable(msgString));
} while(bytesRead == READ_BUF_SIZE);
// emit signal with available data
QDomDocument msg;
if(!msg.setContent(msgString))
qWarning("Error setting content for string %s", qPrintable(msgString));
emit messageReceived(msg);
}
I have see that the message that I don't receive is sent just 3 ms after the previous one...
Send time
[28/04 16:33:30:423] message 1, received
[28/04 16:33:30:525] message 2, received
[28/04 16:33:30:528] message 3, NOT RECEIVED
[28/04 16:33:31:290] message 4, received
This could be a performance issue???
Now I try to solve using standard socket, but I wish to use QTcpSocket because they offer a simple API and for portability reason.
I am using Qt 4.7.1 on linux platform.