[Qt-interest] Stop connection when it's in connecting state

Thiago Macieira thiago.macieira at trolltech.com
Thu Dec 11 08:42:07 CET 2008


alpha_one_x86 wrote:
>In Qt 4.5 it will be possible to stop connection when it's in connecting
>state? With QtcpSocket it's not possible to stop connection state in
> QT4.4.

That depends on what you want. If you call close() or 
disconnectFromHost(), QTcpSocket understands you want to immediately 
disconnect after finishing the connection.

In other words, the following is valid:

	socket->connectToHost("somewhere", someport);
	socket->write("Hello, world");
	socket->disconnectFromHost();
	socket->waitForDisconnected();

That will connect, write and disconnect.

A real use-case scenario is an HTTP/0.9 client:

	socket->connectToHost("trolltech.com", 80);
	socket->write("GET /\r\n");
	// don't call disconnectFromHost, the server disconnects us
	socket->waitForDisconnected();
	qDebug() << socket->readAll();


If you want to disconnect immediately, instead of completing the cycle, 
you can call socket->abort(). That should work in any Qt version, barring 
obviously if any bugs exist.

-- 
Thiago Macieira - thiago.macieira (AT) nokia.com
  Senior Software Engineer - Nokia, Qt Software
  Qt Software is hiring - ask me
      Sandakerveien 116, NO-0402 Oslo, Norway
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20081211/5edb0ab6/attachment.bin 


More information about the Qt-interest-old mailing list