Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, April 2007
QTcpSocket data evaporation issues.


Message 1 in thread

Good Evening,


I'm having some serious issues with QTcpSocket. The problem is that data
seems to evaporate somewhere between writing to a socket using
QDataStream and reading that data at the other end.

I seem to be the only one who's having this problem (google & ML
searches aren't very revealing). I'm using QT 4.2.2.

Basically, I've created a class that wraps up QTcpSocket and turns it
into a blocking socket that reads and writes QByteArrays. I have a
typedef called NSizeType - before any byte arrays gets serialized to the
socket, the size of the byte array is sent. Anyway, perhaps some code
examples.

Writing to the socket is easy:

<code>
void NSocket::write( QByteArray &data )
{
	// create data stream:
	QDataStream ds(m_socket);
	// write the number of bytes we'll be sending to the socket:
	ds << (NSizeType) data.size();
	// then write the actual data:
	ds << data;
}
</code>

Reading is a little more complicated:
<code>
QByteArray NSocket::read()
{
	// make sure there's enough data for our size element:
	while (m_socket->bytesAvailable() < sizeof(NSizeType))
	{
		// not enough data available to extract size type. Keep
blocking
		// until more data arrives.
		//
		// wait for 50 ms:
		m_socket->waitForReadyRead(50);
	}

	// extract the data size:
	//
	// create data stream:
	QDataStream ds(m_socket);
	// create size tyoe variable:
	NSizeType dataSize;
	// extract size from data stream:
	ds >> dataSize;

	// wait till all data has arrived:
	while (m_socket->bytesAvailable() < dataSize)
	{
		m_socket->waitForReadyRead(50);
	}
	// extract input:
	ds >> m_inputArray;
	
	// return copy of input array:
	return m_inputArray;
}
</code>

I've made sure that the socket is in the Connected state - the
constructor blocks until this is the case. A client can send some data
like this:

<code>
QString str("Testing Testing 123 - this is some dummy data...");
myBlockingSocket.write(str.toAscii());
</code>

However, when trying to read that data on the server end, the read()
call blocks in the first while call. QTcpSocket::bytesAvailable()
*always* returns 0.

I'm at my wits end here - I've made sure that the socket is connected,
and the sockets don't return any error.. I hope someone here can point
me in the right direction.


Any ideas?


Cheers,

-- 
 [ signature omitted ] 

Message 2 in thread

Hi,

> I'm at my wits end here - I've made sure that the socket is connected,
> and the sockets don't return any error.. I hope someone here can point
> me in the right direction.
> 
> 
> Any ideas?

I had a quick look at the code and its doesn't look much different from 
the network/blockingfortuneclient example:
http://doc.trolltech.com/4.2/network-blockingfortuneclient-fortunethread-cpp.html

Perhaps you could start from the working example and modify it to fit 
your needs until it doesn't work anymore? That should give a clue. At 
least we'd have a running example that reproduces the problem.

--
 [ signature omitted ] 

Message 3 in thread

> 
> Perhaps you could start from the working example and modify it to fit
> your needs until it doesn't work anymore? That should give a clue. At
> least we'd have a running example that reproduces the problem.
> 

I've managed to narrow down the problem - The qt docs for the
waitForReadyRead() method state that:

"If called from within a slot connected to the readyRead() signal,
readyRead() will not be reemitted."

However, it seems that this is not always the case.. I'm working on some
example code that reproduces the problem reliably.. will let you know
more when I figure it out.


Thanks,

-- 
 [ signature omitted ]