建立一個多線程的QTcpServer

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開始,一個接受若干個連接的可是一次只回答一個.這個服務有一個基本的結構由下面的方法組成:函數

  1. Constructor (without nothing to do).oop

    構造函數this

  2. 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.線程

  3. 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:

  1. 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.

  2. The thread finished notification method that informs the application that the client is disconnected.

  3. 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:

  1. 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.

  2. The readyRead method that reads the information from client and acts depending of the message type.

Further work:

The steps I want to fulfil now are the following:

  1. Create a Protocol class for the messages sent by clients in order to be more efficient.

  2. 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!

相關文章
相關標籤/搜索