Results 1 to 5 of 5

Thread: QTcpServer not handling connections.

  1. #1
    Join Date
    Nov 2008
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTcpServer not handling connections.

    I've been having trouble getting my client/server code to work. Here's a little test program I put together to test my sanity.

    server.hpp:
    Qt Code:
    1. #include <QObject>
    2. class QTcpServer;
    3.  
    4. class Server : public QObject {
    5. Q_OBJECT
    6. public:
    7. Server();
    8. bool done();
    9. private slots:
    10. void newConnection();
    11. private:
    12. bool d;
    13. };
    To copy to clipboard, switch view to plain text mode 

    server.cpp:
    Qt Code:
    1. #include "server.hpp"
    2. #include <QTcpServer>
    3. #include <QTcpSocket>
    4.  
    5. Server::Server() {
    6. d = false;
    7. s = new QTcpServer(this);
    8. s->listen(QHostAddress::Any, 123456);
    9. connect(s,SIGNAL(newConnection()),
    10. this,SLOT(newConnection()) );
    11. }
    12.  
    13. bool Server::done() {
    14. return d;
    15. }
    16.  
    17. void Server::newConnection() {
    18. QTcpSocket * sock = s->nextPendingConnection();
    19. qWarning() << "Got one.";
    20. delete sock;
    21. }
    To copy to clipboard, switch view to plain text mode 

    servermain.cpp:
    Qt Code:
    1. #include "server.hpp"
    2.  
    3. int main() {
    4. Server s;
    5. while (!s.done()) {
    6. }
    7. return 0;
    8. }
    To copy to clipboard, switch view to plain text mode 

    clientmain.cpp:
    Qt Code:
    1. #include <QTcpSocket>
    2. #include <iostream>
    3.  
    4. int main() {
    5. char c;
    6. QTcpSocket * sock;
    7.  
    8. while (std::cin >> c) {
    9. switch(c) {
    10. case 'c':
    11. sock = new QTcpSocket();
    12. sock->connectToHost("127.0.0.1",123456);
    13. if(sock->waitForConnected()) {
    14. qWarning() << "OK";
    15. }
    16. else {
    17. qWarning() << "NO";
    18. }
    19. delete sock;
    20. };
    21. }
    22. return 0;
    23. }
    To copy to clipboard, switch view to plain text mode 

    If I fire up both programs in separate terminals, the client coughs out "OK" every time you enter a 'c', but the server never does anything, and in a debugger the newConnection slot is never executed. What's going on?

    Thanks already.

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

    Default Re: QTcpServer not handling connections.

    123456 is probably an invalid port number. Try something smaller like 55555 for testing.

  3. #3
    Join Date
    Nov 2008
    Posts
    38
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTcpServer not handling connections.

    Well I feel kinda dumb for having an out-of-range port, but with that fixed the behaviour's the same.

  4. #4
    Join Date
    Oct 2008
    Posts
    70
    Thanks
    1
    Thanked 9 Times in 9 Posts

    Default Re: QTcpServer not handling connections.

    2 spraff: If I don't miss my guess, the total number of network ports is 65534.

    Do you have a firewall? May be, it blocked your connection?

    //updated

    Ahh, I looked at your code.

    1) You don't use QCoreApplication (or QApplication) instance in your applications
    2) It's bad idea to use infinity loop. Use qApp->quit() for that.

    Try this code:

    Qt Code:
    1. #include "server.hpp"
    2.  
    3. int main(int argc, char **argv)
    4. {
    5. QCoreApplication app(argc, argv);
    6. Server s;
    7. return app.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QTcpSocket>
    2. #include <iostream>
    3.  
    4. int main(int argc, char **argv) {
    5. QCoreApplication app(argc, argv);
    6. char c;
    7. QTcpSocket * sock;
    8.  
    9. while (std::cin >> c) {
    10. switch(c) {
    11. case 'c':
    12. sock = new QTcpSocket();
    13. sock->connectToHost("127.0.0.1",123456);
    14. if(sock->waitForConnected()) {
    15. qWarning() << "OK";
    16. }
    17. else {
    18. qWarning() << "NO";
    19. }
    20. delete sock;
    21. };
    22. }
    23. return app.exec();
    24. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by pastor; 4th November 2008 at 12:44.

  5. The following user says thank you to pastor for this useful post:

    spraff (11th November 2008)

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

    Default Re: QTcpServer not handling connections.

    True... you do need a running QEventLoop for Qt to process network events and send you signals. So do put a QCoreApplication into your main() as suggested by prastor.

  7. The following user says thank you to caduel for this useful post:

    spraff (11th November 2008)

Similar Threads

  1. QTcpServer limit for incoming connections
    By mdecandia in forum Qt Programming
    Replies: 9
    Last Post: 5th May 2007, 22:09
  2. Replies: 1
    Last Post: 5th July 2006, 14:12

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
  •  
Qt is a trademark of The Qt Company.