Results 1 to 3 of 3

Thread: Having problems with getting QtpServer to Listen

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default Having problems with getting QtpServer to Listen

    I have a real simple application where I want to have a TCP server running on Ubuntu 12.04. I have the following code:

    Qt Code:
    1. WCServer server;
    2. if (server.listen(QHostAddress::Any, 3912)) {
    3. write("Able to listen on port: " + QString::number(server.serverPort()));
    4. } else {
    5. write("Failed to bind to port 3912.");
    6. }
    7. if (server.isListening()) {
    8. write("Server is Listening on port " + QString::number(server.serverPort()));
    9. } else {
    10. write("Server is not Listening.");
    11. }
    To copy to clipboard, switch view to plain text mode 

    WCServer is a subclass of QTcpServer here is the code for it.
    WCServer.h:
    Qt Code:
    1. #ifndef WCSERVER_H
    2. #define WCSERVER_H
    3.  
    4. #include <QTcpServer>
    5. #include "clientsocket.h"
    6. #include "mainwindow.h"
    7. #include "globals.h"
    8.  
    9. class WCServer : public QTcpServer
    10. {
    11. Q_OBJECT
    12. public:
    13. explicit WCServer(QObject *parent = 0);
    14.  
    15. private:
    16. void incomingConnection(int socketID);
    17. MainWindow *w;
    18.  
    19. signals:
    20.  
    21. public slots:
    22.  
    23. };
    24.  
    25. #endif // WCSERVER_H
    To copy to clipboard, switch view to plain text mode 

    This code runs and it say it is listening on port 3912, but when I use any of the following commands it does not show that that port is listening.

    sockstat -l
    netstat -lnptu

    I checked with ufw and the firewall is not on.

    I have tried to reach this port both locally and remotely but it always says "connection refused".

    I have tried to run the program as sudo thinking "maybe a super user is the only one who can listen" but that makes no difference.

    Any help would be greatly appreciated

  2. #2
    Join Date
    Nov 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded

    Default Re: Having problems with getting QtpServer to Listen

    I found out I needed to use new and pass the parent. Odd.

    Qt Code:
    1. server = new WCServer(this);
    2. if (server->listen(QHostAddress::Any, 3912)) {
    3. write("Able to listen on port: " + QString::number(server->serverPort()));
    4. } else {
    5. write("Failed to bind to port 3912.");
    6. }
    7. if (server->isListening()) {
    8. write("Server is Listening on port " + QString::number(server->serverPort()));
    9. } else {
    10. write("Server is not Listening.");
    11. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Having problems with getting QtpServer to Listen

    Is that really so stange? It is difficult for anyone to judge from a fragment of the code. I will try to guess from the information you made available. Since your new code mentions "this" on line 1, it means that it is part of a method's body. Therefore, in your original code, server would go out of scope at the end of the execution of this method and would be destroyed, effectively shutting the server down. The control would subsequently return to the event loop to wait for incoming connections but it was too late since the server had already been destroyed. Now that the server is allocated on the heap, it is not destroyed at the end of the method and this problem disappears. Alternatively you could allocate it on the stack somewhere where it lives long enough (e.g. in main()).

    It would have been useful to see the whole code from the beginning.

Similar Threads

  1. Replies: 1
    Last Post: 23rd April 2011, 09:42
  2. Do you listen music while programming?
    By Raccoon29 in forum General Discussion
    Replies: 20
    Last Post: 30th January 2011, 08:49
  3. What groups, bands do you listen to?
    By ComaWhite in forum General Discussion
    Replies: 3
    Last Post: 1st November 2010, 15:44
  4. Replies: 1
    Last Post: 24th November 2008, 09:56
  5. x11Event() can't listen Button1MotionMask?
    By zeki709 in forum Qt Programming
    Replies: 2
    Last Post: 1st July 2007, 12:19

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