How it works...

There are mainly two types of network connections— the Transmission Control Protocol (TCP) connection and the User Datagram Protocol (UDP) connection. TCP is a reliable networking connection, while UDP is unreliable.

These two connections are designed for very different purposes:

  • TCP networking is usually for programs that require every single piece of data to be sent and received in order. It also makes sure that the client receives the data and that the server gets notified for that. Programs like messaging software, web servers and databases use TCP networking.
  • UDP networking, on the other hand, does not require constant handholding between the server and client. Since the connection is unreliable, there is also no feedback on whether the data has been successfully received. Dropping of packet is tolerated, and data may not even come in the same order as it was sent. UDP connections are usually used by applications that stream huge amounts of data to their clients without strict requirements on its packet delivery, such as video games, video conferencing software, and domain name systems.

Creating networking software using Qt 5 is a lot easier through its signals and slots mechanism. All we need to do is to connect the signals emitted by the QTcpServer class and QTcpSocket class to our slot functions. We will then implement these slot functions and define what to do within those functions.

We used a QVector container to store the pointers to all the clients that have connected to the server so that we can use it to deliver the messages later on.

To keep this example project simple, we simply send text messages to all the clients, sort of like a group chat. You are free to explore other possibilities and make your own changes to improve the program.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset