QTcpSocket
and QTcpServer
are two classes for sockets used in Qt. They work in much the same way as your web browser and a WWW server. These connect to a network address host, whereas QLocalSocket
and QLocalServer
connect to a local file descriptor.
Let's look at QLocalServer
and QLocalSocket
first.
In socket server programming, the basic procedure is as follows:
- Create a socket
- Set socket options
- Bind a socket address
- Listen for connections
- Accept new connection
Qt simplifies these steps to the following:
- Create a socket
- Listen for connections
- Accept new connection
If you need communication on the same machine, then QLocalServer
will be slightly more performant than using a TCP-based socket server. It can be used for Inter-process communication (IPC).
First, we create the server, and then call the listen
function with a string name that clients use to connect. We hook up to the newConnection
signal, so we know when a new client connects.