Results 1 to 3 of 3

Thread: Need Help with QTcpSocket

  1. #1
    Join Date
    Oct 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Need Help with QTcpSocket

    Hi,

    I have a very simple client inspired from the fortuneclient. But for some reason, my readFortune never gets called. It connects, no errors but the slot is never called. It's as if it never gets any data.

    Using nc I am able to see data streaming from the server. The host and port most definitely exist.

    QuarqdClient is of type QObject...

    Any ideas ?

    Thanks

    J

    Qt Code:
    1. #include <QtNetwork>
    2.  
    3. #include "QuarqdClient.h"
    4.  
    5.  
    6. QuarqdClient::QuarqdClient()
    7. {
    8. tcpSocket = new QTcpSocket(this);
    9. connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
    10. connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
    11. this, SLOT(displayError(QAbstractSocket::SocketError)));
    12. requestNewFortune();
    13. //readFortune();
    14.  
    15. }
    16.  
    17. void QuarqdClient::requestNewFortune()
    18. {
    19. blockSize = 0;
    20. tcpSocket->abort();
    21. tcpSocket->connectToHost("192.168.1.2", 8168);
    22.  
    23. }
    24.  
    25. void QuarqdClient::readFortune()
    26. {
    27.  
    28. qDebug() << "Reading!!";
    29. QDataStream in(tcpSocket);
    30. in.setVersion(QDataStream::Qt_4_0);
    31.  
    32. if (blockSize == 0) {
    33. if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
    34. return;
    35. in >> blockSize;
    36. }
    37.  
    38. if (tcpSocket->bytesAvailable() < blockSize)
    39. return;
    40.  
    41. QString nextFortune;
    42. in >> nextFortune;
    43. qDebug() << in;
    44.  
    45. }
    46.  
    47. void QuarqdClient::displayError(QAbstractSocket::SocketError socketError)
    48. {
    49. switch (socketError) {
    50. case QAbstractSocket::RemoteHostClosedError:
    51. break;
    52. case QAbstractSocket::HostNotFoundError:
    53. qDebug() << "Host Not Found";
    54. break;
    55. case QAbstractSocket::ConnectionRefusedError:
    56. qDebug() << "Connection Refused";
    57. break;
    58. default:
    59. qDebug() << tcpSocket->errorString();
    60. }
    61.  
    62. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Need Help with QTcpSocket

    Have you made sure that the connect-calls really work (check the return codes)?

    You might also want to monitor network traffic, or try telnet to check if connecting to that port produces any output (telnet 192.168.1.2 8168).

  3. #3
    Join Date
    Oct 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Need Help with QTcpSocket

    Ok I figured it out. I wasn't setting up my constructor correctly.

    Thanks!

    J

Similar Threads

  1. QTcpSocket proxy authentication problem
    By arbi in forum Qt Programming
    Replies: 3
    Last Post: 14th September 2009, 17:43
  2. Challenging QTcpSocket reliability problem
    By Raccoon29 in forum Qt Programming
    Replies: 3
    Last Post: 13th January 2009, 11:38
  3. array of pointers to QtcpSocket
    By gQt in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 16th June 2008, 10:15
  4. QTcpSocket and libssh2 ...
    By sandros in forum Qt Programming
    Replies: 6
    Last Post: 21st November 2007, 23:34
  5. Strange QTcpSocket behavior (debugged with Ethereal)
    By mdecandia in forum Qt Programming
    Replies: 23
    Last Post: 28th May 2007, 21:44

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.