QT Qthread Learning (ii)

Source: Internet
Author: User

Learning Qthread is mainly to imitate the VC under the FTP server to write a QT version. Not much to say,.

  

The software structure of the FTP server has been explained in the above analysis, the solution today is to let each client process can be a thread to run alone. First, the above 3 classes of CPP file, and then give the phenomenon.

1, Qlistensocket class

1#include"qlistensocket.h"2#include <QTcpSocket>3#include <QDebug>4 5Qlistensocket::qlistensocket (Qobject *parent,intport): Qtcpserver (parent)6 {7 Listen (qhostaddress::localhost,port);8 }9 Ten voidQlistensocket::incomingconnection (intsocketdescriptor) One { AQdebug () <<"A new Client connnected!"; -  -Qcontrolthread *tmp =NewQcontrolthread (Socketdescriptor, This); the clientlist.append (TMP); -  -Tmp->start (); -}
View Code

2, Qcontrolthread class

1 //Thread Constructors2Qcontrolthread::qcontrolthread (qintptr Socketdescriptor,qobject *parent): Qthread (parent)3 {4Qdebug () <<"qcontrolthread Construct Function threadid:"<<Qthread::currentthreadid ();5 6M_controlsocketobj =NewQcontrolsocketobj (Socketdescriptor,0);7 8M_controlsocketobj->movetothread ( This);//move the m_controlsocketobj and its slot functions to this thread to handle9 }Ten  Oneqcontrolthread::~Qcontrolthread () A { -     Deletem_controlsocketobj; -  the quit (); - wait (); - Deletelater (); -}
View Code

3, Qcontrolsocketobj class

1#include"qcontrolsocketobj.h"2#include <QDebug>3#include <QThread>4 5Qcontrolsocketobj::qcontrolsocketobj (qintptr Socketdescriptor,qobject *parent): Qobject (parent)6 {7M_controlsocket =NewQtcpsocket;8M_controlsocket->Setsocketdescriptor (socketdescriptor);9 TenConnect (m_controlsocket,signal (readyread)), This, SLOT (datareceived ())); One  AQdebug () <<"qcontrolsocketobj Construct Function threadid:"<<Qthread::currentthreadid (); - } -  the voidqcontrolsocketobj::d atareceived () - { -Qdebug () <<"qcontrolsocketobj datareceived Function threadid:"<<Qthread::currentthreadid (); -}
View Code

The results appear as follows:

This is the connection of the client, connected to 2 TCP clients, connected to the later, to the server randomly sent some text.

  

2, the application output results.

  

The basic idea has been achieved, or there are several places worth noting. The constructor of the Qcontrolthread is executed in the main thread, and the Qcontrolsocketobj constructor is also executed in the main thread.

The official description of Qobject::movetothread is:

Changes the thread affinity for this object and its children. The object cannot is moved if it has a parent. Event processing'll continue in the targetthread.

Both the Qcontrolthread object and the Qcontrolsocketobj object are constructed in the Qlistensocket class, so their constructors are executed in the main thread.

The thread represented by Qthread is the code whose member functions run execution. The default processing for run is to receive and process messages.

QT Qthread Learning (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.