QT network disk project actual combat

read configuration file

Prepare the configuration file and load the configuration file: IP PORT

The client actively connects to the server, obtains the address of the server through IP, and realizes end-to-end communication through PORT.

The server listens to the local IP and waits for the client to connect.

Here, in order to separate the configuration parameters and options of the application from the code, it provides better flexibility and maintainability. Just imagine finding the variables defined in the code from the code and then modifying them, which is more convenient than directly finding the configuration file to modify?

client.config    //客户端配置文件


127.0.0.1
8888
server.config    //服务端配置文件

127.0.0.1
8888

Set the variable storage IP and PORT (m_strIP and m_usPort) in tcpclient.h, and load the value of the variable through the loadConfig() function.

Client server build

Computer communication needs to pass a certain protocol. Both the client and the server must follow this protocol to communicate stably and securely. The communication protocol of this project is as follows:

struct PDU{
    uint uiPDULen;      //消息分配的空间
    uint uiMsgType;     //消息类型
    char caData[64];    //存放文件名
    uint uiMsgLen;      //实际消息长度
    int csMsg[];        //存放实际消息
};

The space occupied by the PDU here is uncertain, because csMsg is used to open up real space based on information to store actual messages. Therefore, for a PDU, the total occupied space is the space actually opened up by sizeof(PDU)+csMsg. (sieof(PDU) only contains the memory space occupied by the first four items)

sizeof(PDU) = sizeof(uint) + sizeof(uint) + sizeof(char[64]) + sizeof(uint) + sizeof(int*)
           = 4 + 4 + 64 + 4 + 8
           = 84

The specific business process is shown in the following figure:

 If you are confused about TcpServer, MyTcpServer, MyTcpSocker, please see the following picture:

Login Register Exit Logout

All user information is stored in the database, and the database uses sqlite3 instead of MySQL. Add class OpeDB to manipulate the database, in order to support the database need to add `sql` support. The database needs to be connected before the server is executed, so the database connection is set in advance in the main function. The database connection adopts singleton mode.

Click `x` to exit the program flow chart of the client as follows:

1. When there is a connection between client and server, it is bound in the incomingConnection function - when the ofline is triggered, the deleteSocket function is called, which is responsible for deleting the offline socket from the client connection queue.

2. When a client disconnects, the clientOffline function will be triggered, which will call the handleOffline function in OpeDB to modify the `online` field in the database. At the same time, the offline signal is sent, and the signal will trigger the call of deleteSocket (the binding of disconnected and clientOffline is carried out in the constructor of MyTcpSocket).

Interface design: Add book interface and friend interface. When the user successfully logs in, the UI of TcpClient will be hidden and the interface of OpeWidget will be displayed. Depending on the selection -book or friend displays different content.

And for showing online users:

        1. After clicking the button, the showOnline() function is triggered, which sends the request to the server

        2. The server manipulates the database according to the request to obtain the user name whose online field is 1 and sends it back to the client

        3. The client calls the showAllOnlineUsr function according to the returned message type, which further calls showUsr

 Originally, I wanted to draw a function diagram for finding users again. After looking at users, checking online users, adding friends, etc., they all send requests to the server first, then the server replies, and the client does further processing after receiving them. The difference is not big. Just understand it.

As for private chat and group chat, the sender passes the name of the object he wants to communicate to the server, and the server finds the corresponding receiving object through the database and forwards the pdu to the receiving object.

The next step is a series of operations on the file:

After looking at it, it is the interaction between the client and the server and some processing of files. Need to be familiar with the use of some classes of QT, such as QDir file manipulation, etc., the logic is relatively simple.

The source code is as follows: https://download.csdn.net/download/pan_1214_/87811515

Guess you like

Origin blog.csdn.net/pan_1214_/article/details/130726939