Results 1 to 4 of 4

Thread: QTcpSocket->readAll() doesn't have anything to read!

  1. #1
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default QTcpSocket->readAll() doesn't have anything to read!

    I have server and client in the same client/server architecture application.

    First send
    Server::incommingConnection()
    Buffering
    "Task::handleTask strBuffer= taskToHandle---received from localhost:1591"

    "RequestManager::send() response SENDING TO: localhost:1591"
    RequestManager::send() CONNECTING...
    RequestManager::send() CONNECTED
    RequestManager::send() SENT!
    New client connected


    is debug output


    now my problem is that it stops at New client connected and doesn't goto readyRead() infact even when I explicitely call readyRead() [my slot connected to QTcpSocket's readyRead signal] readAll() has nothing in it

    following diagram is better way of describing of whats happening:

    server_response.png

    Following are some bits of my code:

    Qt Code:
    1. void Server::incomingConnection(const int socketDescriptor)
    2. {
    3. #if (DEBUGGING)
    4. qDebug()<<"Server::incommingConnection()";
    5. #endif
    6. client=new NetworkObjects::Client(socketDescriptor,parent_);
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. Client::Client(const int socketDescriptor,QObject *parent) :
    2. QObject(parent),
    3. parent_(parent)
    4. {
    5. setSocketDescriptor(socketDescriptor);
    6. //initialize socket
    7. this->tcpSocket = new QTcpSocket(this);
    8. tcpSocket->open(QTcpSocket::ReadWrite);
    9.  
    10. //connect signals/slots of socket
    11. QObject::connect(tcpSocket,SIGNAL(connected()),this,SLOT(connected()));
    12. QObject::connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(disconnected()));
    13. QObject::connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(readyRead()));
    14.  
    15.  
    16. this->tcpSocket->setSocketDescriptor(this->socketDescriptor());
    17. if (this->tcpSocket->waitForReadyRead(kWaitConnectingMillisecond))
    18. qDebug()<<"New client connected";
    19. else
    20. qDebug()<<"Error while connecting client: "+tcpSocket->errorString();
    21. }
    To copy to clipboard, switch view to plain text mode 

    printing this tcpSocket->errorString() gives error "The remote host closed the connection".
    I even tried to comment out all closing statements that closes socket (for debugging purpose) but didn't do

    Qt Code:
    1. void Client::readyRead(void)
    2. {
    3. qDebug()<<"Buffering";
    4. NetworkObjects::Task *task=new NetworkObjects::Task(parent_,tcpSocket->readAll());
    5. QObject::connect(task,SIGNAL(taskCompleted()),task,SLOT(deleteLater()));
    6. task->handleTask();
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

    see the app instance 1 in above image sends request successfully and app instance 2 receives it fine and creates response

    Qt Code:
    1. void RequestManager::send(QByteArray *request)
    2. {
    3. #if (DEBUGGING)
    4. qDebug()<<"RequestManager::send() " + QString(*request) + " SENDING TO: "+address_+":"+QString::number(port_);
    5. #endif
    6. sendTcpSocket->abort();
    7. sendTcpSocket->open(QTcpSocket::ReadWrite);
    8.  
    9. sendTcpSocket->connectToHost(address_,port_);
    10. #if (DEBUGGING)
    11. qDebug()<<"RequestManager::send() CONNECTING...";
    12. #endif
    13.  
    14. if (sendTcpSocket->waitForConnected(3000))
    15. {
    16. #if (DEBUGGING)
    17. qDebug()<<"RequestManager::send() CONNECTED";
    18. #endif
    19. }
    20.  
    21.  
    22. if (sendTcpSocket->isOpen()==false){
    23. emit socketCannotOpen();
    24. }
    25. else if (sendTcpSocket->isWritable())
    26. {
    27.  
    28.  
    29. sendTcpSocket->write(*request);
    30.  
    31. #if (DEBUGGING)
    32. qDebug()<<"RequestManager::send() SENT!";
    33. #endif
    34. }
    35.  
    36. // sendTcpSocket->close();
    37.  
    38. }
    To copy to clipboard, switch view to plain text mode 


    can we please try to help me out?

    thanks
    Life is like a dream, sometimes it is good and somtimes it is bad, but in the end it is over

  2. #2
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QTcpSocket->readAll() doesn't have anything to read!

    no one?

    can someone please reply (only if you know the answer)?
    Life is like a dream, sometimes it is good and somtimes it is bad, but in the end it is over

  3. #3
    Join Date
    Feb 2011
    Posts
    354
    Thanks
    17
    Thanked 27 Times in 24 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows

    Default Re: QTcpSocket->readAll() doesn't have anything to read!

    Maybe you should call waitForConnected before calling waitForReadyRead. Also consider non-blocking approach.

  4. #4
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTcpSocket->readAll() doesn't have anything to read!

    Attached is simple client-server demo app, see if it works for you and try to resolve your issue based on it.
    Attached Files Attached Files

Similar Threads

  1. QTcpSocket, problems with readall()
    By Leo_san in forum Qt Programming
    Replies: 2
    Last Post: 28th September 2011, 23:31
  2. QTcpSocket can't read all bytes
    By Qiieha in forum Qt Programming
    Replies: 27
    Last Post: 23rd August 2011, 15:48
  3. QTcpSocket crashes on read
    By xenome in forum Qt Programming
    Replies: 9
    Last Post: 8th May 2011, 22:25
  4. How to obtain a QI,age from QTcpSocket->readAll
    By gorka_sm in forum Qt Programming
    Replies: 1
    Last Post: 27th April 2011, 08:50
  5. Unable to read from QTcpSocket
    By jimroos in forum Qt Programming
    Replies: 1
    Last Post: 4th July 2007, 21:09

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.