PDA

View Full Version : QTcpSocket::connectToHost() is blocking the GUI!



jackmack
16th December 2010, 10:58
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

wysota
16th December 2010, 11:13
Please provide a minimal compilable example reproducing the problem.

jackmack
17th December 2010, 10:49
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

wysota
17th December 2010, 12:39
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.

jackmack
17th December 2010, 12:54
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

wysota
17th December 2010, 13:55
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.

jackmack
20th December 2010, 12:22
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?

arnaiz
4th January 2011, 17:14
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.

jackmack
10th January 2011, 10:48
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.

jackmack
18th January 2011, 14:46
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.