[Qt-interest] QTcpSocket Problems to transfer data

Thiago Macieira thiago at kde.org
Mon Jan 17 20:11:20 CET 2011


On Monday, 17 de January de 2011 10:06:02 Gustavo Walbon wrote:
> *//Client*
> void RemoteAcessClient::readStreaming(QString Address, int Socket)
> {
>     blockSize = 0;
>     TcpSocket->abort();
> 
>     TcpSocket->connectToHost(Address,Socket);

At this point, the socket is still connected to the remote host. It has not 
finished connecting. There is definitely no data to be read -- bytesAvailable() 
== 0.

> 
>     QDataStream in(TcpSocket);
>     in.setVersion(QDataStream::Qt_4_6);
> 
>     DataNode Dados;
> 
>     if (blockSize == 0) {
>         if (TcpSocket->bytesAvailable() < (int)sizeof(quint16))
>             return Dados;

You've most likely exited the function here. The rest of the code is never 
executed.

>         in >> blockSize;
>     }
> 
>     if (TcpSocket->bytesAvailable() < blockSize)
>         return Dados;
> 
>     in >>  Data1 ;       // Float Data
>     in >>  Data2 ;       // Float Data
>     in >>  Data3 ;       // Float Data
>     in >>  Data4 ;       // Float Data
> 
> }
> 
> ////////////////////////////////////////////////////////////////////////////
> ///////////////////////////////////////////
> 
> *//Server*
> 
> void RemoteAcessServerThread::run()

Please stop using threads for this.

> {
>     QTcpSocket tcpSocket;
> 
>     if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
>         emit error(tcpSocket.error());
>         return;
>     }
> 
>     QByteArray block;
>     QDataStream out(&block, QIODevice::WriteOnly);
>     out.setVersion(QDataStream::Qt_4_6);
>     out << (quint16)0;
> 
>     out << Data1 ;       // Float Data
>     out << Data2 ;       // Float Data
>     out << Data3 ;       // Float Data
>     out << Data4 ;       // Float Data
> 
>     out.device()->seek(0);
>     out << (quint16)(block.size() - sizeof(quint16));
> 
>     tcpSocket.write(block);
>     tcpSocket.disconnectFromHost();
>     tcpSocket.waitForDisconnected();
> }

This one looks like it sends the data just fine.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Senior Product Manager - Nokia, Qt Development Frameworks
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110117/308d9025/attachment.bin 


More information about the Qt-interest-old mailing list