Results 1 to 3 of 3

Thread: QTcpSocket - How do I know when it is disconnected ?

  1. #1
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTcpSocket - How do I know when it is disconnected ?

    I have a thread that handles different connections from different computers.

    I want to know when one of those connections (telnet) has been closed. I am trying to use the disconnect signal, but the method clientDisconnected is never called.

    Qt Code:
    1. # include <QTcpSocket>
    2.  
    3. # include <iostream>
    4. # include "client.h"
    5. # include "server.h"
    6. using namespace std;
    7.  
    8. Client :: Client(int socketDescriptor, Server *server, QObject *parent) : QThread(parent)
    9. {
    10. this->socketDescriptor = socketDescriptor;
    11. this->server = server;
    12. chattingWith = new QString();
    13. connect(this, SIGNAL(finished()), this, SLOT(finished()));
    14. }
    15.  
    16.  
    17. void Client :: run()
    18. {
    19. cout << "void Client :: run()\n";
    20. tcpSocket = new QTcpSocket();
    21. if(!tcpSocket->setSocketDescriptor(socketDescriptor))
    22. {
    23. cout << "Error - tcpSocket descriptor\n";
    24. return;
    25. }
    26. connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
    27. connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
    28. exec();
    29. }
    30.  
    31.  
    32. void Client :: readData()
    33. {
    34. cout << "void Client :: readData()\n";
    35. server->addClient("12345", this);
    36. }
    37.  
    38.  
    39. void Client :: clientDisconnected()
    40. {
    41. cout << "void Client :: clientDisconnected()\n";
    42. }
    43.  
    44. void Client :: finished()
    45. {
    46. cout << "The thread has finished\n";
    47. }
    To copy to clipboard, switch view to plain text mode 

    Why ? What is wrong ?

  2. #2
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket - How do I know when it is disconnected ?

    Why is the QTcpSocket disconnect signal never emitted ?

  3. #3
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket - How do I know when it is disconnected ?

    I found the problem:

    The signal was never emitted because I was only compiling the program with "make". In order to have the signal working I had to type "qmake" first, then "make".

    Out of this test I assume that whenever I code something with SIGNALS and SLOTS, then I will "qmake" my program first.

Similar Threads

  1. Challenging QTcpSocket reliability problem
    By Raccoon29 in forum Qt Programming
    Replies: 3
    Last Post: 13th January 2009, 10:38
  2. array of pointers to QtcpSocket
    By gQt in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 16th June 2008, 09:15
  3. QTcpSocket and libssh2 ...
    By sandros in forum Qt Programming
    Replies: 6
    Last Post: 21st November 2007, 22:34
  4. Strange QTcpSocket behavior (debugged with Ethereal)
    By mdecandia in forum Qt Programming
    Replies: 23
    Last Post: 28th May 2007, 20:44
  5. QTcpSocket disconnection problem
    By erdi in forum Qt Programming
    Replies: 4
    Last Post: 19th February 2006, 21:50

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.