Improvements
The example we explained uses a nonblocking, asynchronous approach. For example, after asynchronous calls such as connectToHost()
, we do not block the thread until we get a result, but instead, we connect to the socket's signals to proceed. On the Internet as well as Qt's documentation, on the other hand, you will find dozens of examples explaining the blocking and the synchronous approaches. You will easily spot them by their use of waitFor...()
functions. These functions block the current thread until a function such as connectToHost()
has a result—the time connected()
or error()
will be emitted. The corresponding blocking function to connectToHost()
is waitForConnected()
. The other blocking functions that can be used are waitForReadyRead()
, which waits until new data is available on a socket for reading; waitForBytesWritten()
, which waits until the data has been written to the socket; and waitForDisconnected()
, which waits until the connection has been closed...