What just happened?

The m_socket private member variable holds an instance of QTcpSocket. If this socket is already connected, nothing happens. Otherwise, the socket is connected to the given address and port by calling connectToHost(). Besides the obligatory server address and port number, you can pass a third argument to define the mode in which the socket will be opened. For possible values, you can use OpenMode just like we did for QIODevice.

Since this call is asynchronous, we print a notification to the chat so that the user is informed that the application is currently trying to connect to the server. When the connection is established, the socket sends the connected() signal that prints "Connected to server" on the chat to indicate ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.