Results 1 to 13 of 13

Thread: QTcpServer(this)

  1. #1
    Join Date
    Feb 2018
    Posts
    22
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default QTcpServer(this)

    Hello everyone.

    I have been assigned with the responsibility of maintaining some old Qt program.
    In the process i had to convert it from Qt 4 to Qt 5.10 and from Qwt 4.x.x to Qwt 6.1.3

    Everything seemed to work and i can compile and run the program.
    However, when the server is set up to listen on the corresponding ports, new_Connection() is never called.

    I get the following warning:
    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is gui::model::Listener(0x2ba442c0), parent's thread is QThread(0x215f97b8), current thread is QThread(0x2ba445b0)
    More specifically (see code below) the line
    Qt Code:
    1. server_ = new QTcpServer(this);
    To copy to clipboard, switch view to plain text mode 
    is causing it.
    I have a strong feeling that it's because
    Qt Code:
    1. Listener
    To copy to clipboard, switch view to plain text mode 
    is not in the same thread as
    Qt Code:
    1. server_
    To copy to clipboard, switch view to plain text mode 

    I have search the internet thin, but I cannot seem to find any post that has relevance (or they are too old)

    Qt Code:
    1. #include <QtNetwork>
    2. #include <QtDebug>
    3. #include "listener.h"
    4. #include "sockethandler.h"
    5. #include <qthread.h>
    6.  
    7. // Assertion to ensure that char and short has the expected sizes
    8. // (required for protocol to work)
    9.  
    10. namespace gui { namespace model {
    11. Listener::Listener(QObject *parent, int port) :
    12. QObject(parent), port_(port), server_(0)
    13. { }
    14.  
    15. /**
    16.   * Create server and listen for connections
    17.   */
    18. void Listener::listen() {
    19. if (server_!=0) qFatal("listen() called on a connection that was already initialized");
    20. server_ = new QTcpServer(this);
    21. server_->listen(QHostAddress::Any, port_);
    22. qDebug() << connect(server_, SIGNAL(newConnection()),
    23. this, SLOT(new_Connection()));
    24. emit log(tr("Listening on port %1").arg(port_));
    25. }
    26.  
    27. /**
    28.   * Called on new connection.
    29.   * Check for duplicate and initialize socket.
    30.   */
    31. void Listener::new_Connection()
    32. {
    33. while (server_->hasPendingConnections()) {
    34. emit log(tr("Connection opened on port %1").arg(port_));
    35. emit connected(
    36. new SocketHandler(this, server_->nextPendingConnection())
    37. );
    38. }
    39. }
    40. }} // gui::model
    To copy to clipboard, switch view to plain text mode 

    Writing
    Qt Code:
    1. server_ = new QTcpServer();
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. server_ = new QTcpServer(this);
    To copy to clipboard, switch view to plain text mode 
    removes the warning, but the problem persists.

    .h file:
    Qt Code:
    1. #ifndef CONNECTION_H
    2. #define CONNECTION_H
    3.  
    4. #include <QObject>
    5.  
    6. #include "connectassert.h"
    7.  
    8. QT_BEGIN_NAMESPACE
    9. class QTcpServer;
    10. class QTcpSocket;
    11. QT_END_NAMESPACE
    12.  
    13. namespace gui { namespace model {
    14. class SocketHandler;
    15. /**
    16.   * Listens on a single port for a connection from HAsim.
    17.   */
    18. class Listener : public QObject {
    19. Q_OBJECT
    20. public:
    21. /** Creates object */
    22. explicit Listener(QObject *parent, int port);
    23. /** Listens for connections */
    24. void listen();
    25.  
    26. signals:
    27. /** Emitted when a connection is opened */
    28. void connected(SocketHandler*);
    29. /** Emitted when there is information available about connection */
    30. void log(QString str);
    31.  
    32. private slots:
    33. void new_Connection();
    34.  
    35. private:
    36. int port_;
    37.  
    38. QTcpServer *server_;
    39. };
    40.  
    41. }} // gui::model
    42.  
    43. #endif // CONNECTION_H
    To copy to clipboard, switch view to plain text mode 

    So how can I correctly fix the error?

    EDIT: I forgot to add - This worked before (on Qt4), so what is changed from Qt4 to Qt5.10 that would cause this to fail?

    I hope you can help

    best regards
    David
    Last edited by DAVC; 19th March 2018 at 14:06. Reason: updated contents

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer(this)

    I would say that your SocketHandler creation is probably taking place in a different thread.
    Try this see if it helps:
    Qt Code:
    1. void Listener::new_Connection()
    2. {
    3. while (server_->hasPendingConnections()) {
    4. emit log(tr("Connection opened on port %1").arg(port_));
    5. emit connected(
    6. new SocketHandler(nullptr, server_->nextPendingConnection())
    7. );
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    If this gets rid of the error, the you still will have to write some code to take care of the SocketHanlder deletion when you don't need it any more.

    Alternatively, the other thread issue is in your SocketHandler.
    Can you show your SocketHandler code?
    Last edited by high_flyer; 20th March 2018 at 10:54.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2018
    Posts
    22
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTcpServer(this)

    Thank you High_Flyer

    I can try, but I don't think it will help, as "new_Connection()" is never called. The signal "newConnection" is never emitted.

    I should probably elaborate a little further.
    The program written in Qt is a GUI (lets call it ServerGUI) meant for measuring and plotting realtime data delivered by a simulator (lets call it SIM).
    ServerGUI listens on port 3500 and 3501. I have verified this through debugging and netstat.
    Even SIM can see that ServerGUI listens on these ports, thus delivers the data to port 3500 and port 3501, however, it is as if ServerGUI never registers or accepts this connection.

    SIM is a 32-bit program, ServerGUI is built using MingW 32-bit in Qt Creator. I am working on a 64-bit Windows 10 machine. (I am writing this because other people with a "somewhat" same issue, mentioned different bit-versions as a possible problem)

    Best Regards
    David

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer(this)

    Can you show the code surrounding the call to listen() on a Listener instance?
    Is there anything being moved to different threads there?

    Respectively look for all the places in the code threads are involved.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Feb 2018
    Posts
    22
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTcpServer(this)

    Yes,
    here is where listen from Listener is being called:
    Qt Code:
    1. void Server::listen() {
    2.  
    3. dataListener_ = new Listener(this,DATA_PORT);
    4. connectSignals(dataListener_);
    5. dataListener_->listen();
    6.  
    7. ctrlListener_ = new Listener(this,CTRL_PORT);
    8. connectSignals(ctrlListener_);
    9. ctrlListener_->listen();
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

    The hierachy is:
    Main===>model_.listen() ===> server_->listen() ===> Listern->listen()

    If you are talking about Qthread, it is not used anywhere in the code.

    Best Regards
    David

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer(this)

    Based on the code you posted, I can't see where the problem is.
    Probably a wider code scope is needed to see where the problem is.
    Somewhere, use of threads is made - the question is where.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Feb 2018
    Posts
    22
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTcpServer(this)

    Thank you for your time.

    I will try some different solutions.
    I have some leads i will follow (I only work on this project on mondays)

    I will (unlike the many forums i have searched) report back with the solution when i find it, so others who may run into the same problem has a chance.

    Best Regards
    David

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer(this)

    I will (unlike the many forums i have searched) report back with the solution when i find it, so others who may run into the same problem has a chance.
    That will be very appreciated!
    We encourage it here!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Feb 2018
    Posts
    22
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTcpServer(this)

    I have returned as promised.
    I have to be completely honest though, I don't rightly know what exactly fixed it.

    I had some notion that it was related to loading incorrect dll-files (libraries), as I had investigated the possibility of building my application with static linking instead of dynamic linking.
    In my project I also use Qwt 6.1.3.
    So, to make sure there were no lose ends, I recompiled the Qwt library, following the instructions to the letter.
    However, linking to the new location of the newly compiled Qwt library was challenging. Everywhere i searched everyone said the syntax was:
    Qt Code:
    1. LIBS += \
    2. -L$$PWD/../../../Qwt-6.1.3/lib/ \
    3. -lqwtd \
    4. -L$$PWD/../../../Qwt-6.1.3/lib/ \
    5. -lqwt
    6. INCLUDEPATH += $$PWD/../../../Qwt-6.1.3/include
    To copy to clipboard, switch view to plain text mode 
    and to write it in the .pro-file, but for some reason the application kept looking in the old directory (and throwing errors like "cannot find someheaderfile.h"). Writing the above code in the .pri-file sovled the issue.

    I have done all this before naturally, but there must have been one small thing different, because now it works.
    The program crashes - but that is after the connection has been made, so I'm happy.

    EDIT:
    Okay, deleting:
    Qt Code:
    1. qDebug() << connect(server_, SIGNAL(newConnection()),
    2. this, SLOT(new_Connection()));
    To copy to clipboard, switch view to plain text mode 
    in:
    Qt Code:
    1. void Listener::listen() {
    2. if (server_!=0) qFatal("listen() called on a connection that was already initialized");
    3. this->server_ = new QTcpServer();
    4. server_->listen(QHostAddress::Any, port_);
    5. qDebug() << connect(server_, SIGNAL(newConnection()),
    6. this, SLOT(new_Connection()));
    7. emit log(tr("Listening on port %1").arg(port_));
    8. }
    To copy to clipboard, switch view to plain text mode 
    Causes the connection to no longer be made. Now I'm even more befuddled than before.

    Best Regards
    David
    Last edited by DAVC; 26th March 2018 at 12:00.

  10. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTcpServer(this)

    It's a bad idea to have any executable code in a qDebug() statement. It may not be the cause of your connect() problem, but if you need to print the result of the connect() statement, it is better to assign it to a temporary variable then output that variable's value to qDebug().

    I have been burned more than once when I put executable code (i.e. code that executes to produce a side-effect, such as connect()) into an assert() or a conditional and being puzzled why the code suddenly stops working in release mode or under certain conditions, only to find out that the compiler has thrown the code away (as with assert) or optimized it away.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  11. The following user says thank you to d_stranz for this useful post:

    DAVC (27th March 2018)

  12. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer(this)

    Okay, deleting:
    This is in a way related to d_stranz's reply:
    if you are deleting the connection you were doing in the qDebug() then the connection will not be created. (and indeed I see I didn't notice that the connected was in the qDebug() which could have beedn the cause of the connection not to have been made)
    Or I don't understand what you mean with that part of your post.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  13. The following user says thank you to high_flyer for this useful post:

    DAVC (27th March 2018)

  14. #12
    Join Date
    Feb 2018
    Posts
    22
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QTcpServer(this)

    doh! Thank you so much, I was a bit tired when i wrote it.
    I completely missed that it wasn't just a random:
    Qt Code:
    1. qDebug() << "Did we get to this part of the code?"
    To copy to clipboard, switch view to plain text mode 
    Now it makes sense again!

    I usually don't execute statements in a qDebug, this was purely because I at the time was uncertain that the connection ever got established. I just forgot to delete it again.
    I will use your advice to save it in a variable and then pass it to qDebug to avoid a situation like this again, Thank you!

    Best regards David
    Last edited by DAVC; 27th March 2018 at 08:36.

  15. #13
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTcpServer(this)

    Regarding the Qwt setup. Commonly you build Qwt and install it (make install) to put the resulting libs, includes etc into a configurable location (e.g. c:\Qwt-6.1.3). Then the instructions here, with Qwt as a feature, have worked for me. Nothing but "CONFIG += qwt" needed in the PRO file for a build, but the qwt.dll needs to be in the run-time PATH of the process in order for the result to run.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

Similar Threads

  1. QTcpServer
    By shreeja in forum Newbie
    Replies: 1
    Last Post: 28th December 2015, 10:21
  2. QTcpServer
    By DmitryNik in forum Newbie
    Replies: 0
    Last Post: 25th October 2011, 13:36
  3. QTcpServer
    By DmitryNik in forum Newbie
    Replies: 2
    Last Post: 1st October 2011, 09:07
  4. QTcpServer and GDB
    By baray98 in forum Qt Programming
    Replies: 2
    Last Post: 21st January 2009, 09:02
  5. Replies: 1
    Last Post: 18th June 2006, 11: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.