Each week I try to complete some challenge with Qt to improve my skills. Sometimes they are easy tests to remember past knowledge that in other way I forgot, but this week what I wanted was to understand in a basic how QThread runs together with QTcpServer. To do it more difficult, I want that the QTcpServer was allowed to get connections from n clients and depending on the command sent by the client, answer to an one or answer to all the clients connected to the server. In other way a multithread would had no sense… hehehe. The classes I have done are based on the ThreadedFortuneServer example that Qt-Creator has.多线程
... To do it more difficult,我想让QTcpServer能够容许从n个客户端得到连接,依据客户端发来的命令,向一个或全部的已连接客户端发送响应. 另外一方面,多线程应该没法被感知app
I started from a running QTcpServer that accepts several connections but only could answer one at time. This server had a really basic structure for the follwind methods:socket
我从运行一个QTcpServer开始,一个接受若干个连接的可是一次只回答一个.这个服务有一个基本的结构由下面的方法组成:函数
Constructor (without nothing to do).oop
构造函数this
Overloaded incommingConnection(int socketDescription) that creates a QTcpSocket client and stores it in a QMap<int,QTcpSocket>. The int in the map the socketDescriptor. In this method I connect the QTcpSocket::readyRead() method to my own reader method and I connect the QTcpSocket::disconnected() signal to my own method that removes the client from the map.spa
重载的 incommingConnection(int socketDescription)函数,建立QTcpSocket 客户端并保存在一个QMap<int,QTcpSocket>.map中的int是socketDescriptor.在这个方法中,我链接QTcpSocket::readyRead()方法到我本身的读函数,链接 QTcpSocket::disconnected() 信号到我本身的函数,用于从map中删除client.线程
The reader method calls a sendMulticastMessage(const QString &_msg) it was necessary.code
读函数在它须要的状况下调用sendMulticastMessage(const QString &_msg) .orm
Now, I have had to change the single class structure of my application to a more complex structure (two classes, hehehe). The first class is the QTcpServer class in the same way that before. The second class is a QThread that creates the QTcpSocket from where I will receive the incoming messages and which I will use to send my own information to the client.
如今,我须要把个人控制台程序的信号类结构变为一个更复杂的结构(俩个类).第一个类是和以前同样的QTcpServer类.第二个类是一个QThread,它建立QTcpSocket.
In the Server I only have three methods:
The overloaded QTcpServer::incomingConnection(int socketDescriptor) where I receive the connection request from the client: Here is where I create a class instance of ServerThread and connect the QThread::finished() signal to my own notification method. In addition I connect the signal that ServerThread emits when data is received to a own Server method.
The thread finished notification method that informs the application that the client is disconnected.
The slot with the message that ServerThread emites. This slot send a (simulated) multicast message to all the clients.
In the ServerThread I have the following methods:
Overloaded QThread::run(). Here are connected the signals of the socket to read data and delete if it is disconnected. The server writes a welcom message to the client and then starts the loop waiting for readyRead socket signal while it is connected.
The readyRead method that reads the information from client and acts depending of the message type.
The steps I want to fulfil now are the following:
Create a Protocol class for the messages sent by clients in order to be more efficient.
Add a logger that creates a .log file and truncates it in 1 MB files. Must be thread-safe!
I will publish the code when it will be finished, but if anyone wants a copy, contact me without problems by comments or email!