[Qt-interest] Question on QTcpSocket & QTcpServer
Jason H
scorp1us at yahoo.com
Fri Oct 16 23:35:21 CEST 2009
This code makes no sense. DataStreams are sequential, or I thought they were!
And You need to use waitForBytesWritten() if yuo want to serialize it.
You should (perferably) be using a get/response type of protocol that allows you to make reuqests and have them filled later. Then onReceive(), you identify the response type coming back to you. That is optimal. But if you can't do that, then using waitFor..() will work.
But I still don't know why you are using seek() at all!
________________________________
From: David Boosalis <david.boosalis at gmail.com>
To: QtInterest <qt-interest at trolltech.com>
Sent: Fri, October 16, 2009 5:19:19 PM
Subject: [Qt-interest] Question on QTcpSocket & QTcpServer
Hi
I have an application that uses QTcpSocket on the client and the server uses QTcpServer. Everything works fine if I do one request from the client to the server, but I want to do multiple request and this is where it fails.
MyApp::connectedSlot()
{
// ok have caught the signal to tell me I am connected to the server. So now I send my request(s)
QByteArray block;
qint64 numBytes;
QDataStream out(&block,QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16) 0;
out << (quint16) ServerMessage::GetUsers;
out.device()->seek(0);
out << (quint16) (block.size() - sizeof(quint16));
numBytes = serverSocket->write(block);
//I want to send another request and this is where I get an error on the server
block.clear();
out.device()->seek(0);
out << (quint16) 0;
out << (quint16) ServerMessage::GetProfiles;
out.device()->seek(0);
out << (quint16) (block.size() - sizeof(quint16));
numBytes = serverSocket->write(block);
}
If I put debug lines in my client code above I get no errors. However on the serve side I get a socket disconnect followed by a connected and only see the first request for users.
Can I not write to a socket with back to back calls like this. I would think they would be queued. I was looking for something like a socket->isReady() but could not find any such method. Of course I could bundle the two request to the server as one request, but was wondering what I am not understanding about TcpSockets.
Any advise as to why my double request to the serve does not work is appreciated.
I am using Qt 4.6 tpl1 by the way
-David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091016/4a9fe775/attachment.html
More information about the Qt-interest-old
mailing list