Results 1 to 10 of 10

Thread: QTcpSocket::connectToHost() is blocking the GUI!

  1. #1
    Join Date
    Jun 2010
    Posts
    31
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QTcpSocket::connectToHost() is blocking the GUI!

    Hi all,

    I'm using Qt 4.7.0 on XP with VS2008.

    I want to use QTcpSocket to connect to a server to interchange simple xml telegrams.
    Very important is to implement a good reconnect functionality. When I read the docs (examples and forums threads!) I understand the functionality of QTcpSocket is asynchronous which means NO BLOCKING. There are a lot of signals/slots.

    Now here the example:

    Constructor of mySocket class:

    ...
    m_socket = new QTcpSocket();

    connect(m_socket, SIGNAL(readyRead()),
    this, SLOT(slotReadData()));

    connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)),
    this, SLOT(slotError(QAbstractSocket::SocketError)));

    connect(m_socket, SIGNAL(connected()),
    this, SLOT(slotConnected()));

    connect(m_socket, SIGNAL(disconnected()),
    this, SLOT(slotDisconnected()));
    ...

    When I call now m_socket->connectToHost("127.0.0.1", 1234) and there is no socket server running on that port the apps GUI freezes for a short time. And when I call connectToHost(...) inside slotError() and slotDisconnected() the GUI freezes cyclic.

    I thought exactly this behaviour is fixed with signals/slots and means asynchronous?

    I tried also to put the QTcpClient to a thread's run() method. But I don't know how I can implement a reconnect functionality, reading data, ... and all thread-safe.

    Please help me. Thanks a lot.

    -jack

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket::connectToHost() is blocking the GUI!

    Please provide a minimal compilable example reproducing the problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2010
    Posts
    31
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket::connectToHost() is blocking the GUI!

    Hi Master of Zen,

    I have written a small program to reproducing blocking using Qt 4.7.0, VS2008 compiler, XP. You can compile it with nmake.
    The .zip contains also a free socket tool to run as server. The simple example connects as client to it.

    But tell me how I can send a .zip file to you ?! I don't see any options to attach a file :-(

    Regards,
    Jack

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket::connectToHost() is blocking the GUI!

    Quote Originally Posted by jackmack View Post
    But tell me how I can send a .zip file to you ?! I don't see any options to attach a file :-(
    "Go Advanced" and scroll down.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jun 2010
    Posts
    31
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket::connectToHost() is blocking the GUI!

    Hi,

    see attached example. Bu sorry I have to removed the free socket server test tool because it was too big. Hope you have an other tool.

    To see blocking gui it's enough to start the example to connect to a server/port which doesn't running.

    During running the example please moving the example window on your desktop then you can see/feel how the window is blocked.

    Regards
    jackmack
    Attached Files Attached Files

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpSocket::connectToHost() is blocking the GUI!

    You are probably falling into an endless loops as when your connection fails you try to reconnect again. Try removing the reconnect functionality and see if the GUI still gets frozen.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jun 2010
    Posts
    31
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket::connectToHost() is blocking the GUI!

    I think Qt is always falling in endless loop if the server is not available! Try the fortuneclient example of Qt.

    1) Start the fortune client and enter a free port number
    2) Press "Get fortune" and then moving the fortune clients window.

    Result:
    After a short time the window is blocking first time (this is during connectToHost() is not able to establish the connection!).
    The second blocking is done by connected slot displayError() to signal QAbstractSocket::error(). The displayError() calls a blocking QMessageBox(). That's ok.

    I think QTcpSocket is not realy asynchronous! I understand "asynchronous" is when you can moving the window without any freeze moments.

    Any suggestions?

  8. #8
    Join Date
    Mar 2006
    Location
    Mexico City
    Posts
    31
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket::connectToHost() is blocking the GUI!

    Hi jackmack.

    I tried to reproduced the error that you get when using fortune client, and it worked ok in my system. No blocking, and no endless loop. Even when I used another non existing IP, the window can be moved around, until the apperance of the error in connection message.

    I'm using Qt 4.7.0, and win32-g++ with minGW. Maybe the problem is when using VS2008.

    Hope this helps.

  9. #9
    Join Date
    Jun 2010
    Posts
    31
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket::connectToHost() is blocking the GUI!

    Hi,

    wow, that's strange.
    I will try it also again with the Qt intergrated IDE. I think this is the fastest way to test it.

    Thanks a lot.

  10. #10
    Join Date
    Jun 2010
    Posts
    31
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpSocket::connectToHost() is blocking the GUI!

    Quote Originally Posted by arnaiz View Post
    Hi jackmack.

    I tried to reproduced the error that you get when using fortune client, and it worked ok in my system. No blocking, and no endless loop. Even when I used another non existing IP, the window can be moved around, until the apperance of the error in connection message.

    I'm using Qt 4.7.0, and win32-g++ with minGW. Maybe the problem is when using VS2008.

    Hope this helps.
    Hi Arnaiz,

    I have installed also the Qt 4.7.0 with minGW and the result is strange!

    Running the fortuneclient on clean VM system (64 bit) -> non blocking!
    Running the fortuneclient on clean VM host system (64 bit) -> non blocking!
    Running the fortuneclient on a clean windows XP system (PC 32bit) -> non blocking!
    Running the fortuneclient on my develop system (laptop 32bit) -> it's blocking!
    Running the fortuneclient on my old develop system (laptop 32bit) -> it's blocking!

    On my old development system I have uninstalled the Microsoft VisualC++ IDE and the Web'n Walk drivers for UMTS access.
    NOW IT WORKS! NO BLOCKING OCCURS! That's strange.

Similar Threads

  1. Blocking QTcpSocket for read/write operations
    By vcernobai in forum Qt Programming
    Replies: 3
    Last Post: 15th June 2020, 08:43
  2. QTcpSocket::connectToHost
    By yogeshgokul in forum Qt Programming
    Replies: 18
    Last Post: 7th July 2009, 10:42
  3. Timing out a QFtp ConnectToHost
    By skp in forum Qt Programming
    Replies: 10
    Last Post: 27th April 2008, 13:10
  4. QUdpSocket bind / connectToHost ?
    By mnemonic_fx in forum Newbie
    Replies: 2
    Last Post: 17th July 2007, 03:20
  5. QTcpSocket connectToHost memory leak?
    By Lele in forum Qt Programming
    Replies: 9
    Last Post: 6th March 2006, 09:26

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.