QT builds multi-threaded server

Source: Internet
Author: User

The cause is that MySQL has no drivers on Android. In other words, the mobile side wants to access the remote database and must be brokered through one (or more) PCs.

The relay PC, as server, accepts requests from the mobile socket to access the database, the server accesses the database, obtains the data, and sends it to the mobile side via the socket.

QT Write a C/s is actually very simple, online various tutorials, mishap: server! Is! Single! Line! Ride!

Assuming there are 10,000 mobile-side access to the transit server, then if the server is single-threaded, then these 10,000 mobile is queued communication, queued access to the database, definitely finished!

Therefore, the server must use multithreading.

QT Multithreading is often a novice to make mistakes, many articles appear to be multi-threading, in fact, is simply a single thread.

The default c/S connection mode (acceptconnection) does not support multithreading is also a mishap!

So for a long time, finally fixed the multi-threaded server.

① first writes the server class, derived from the Qtcpserver, as long as the overloaded incomingconnection This virtual function is OK.

No need to connect (&server,signal (Newconnection ()), This,slot (Acceptconnection () )) as a single thread;

void server::incomingconnection (qintptr socketdescriptor) {    new socketthread (Socketdescriptor,  This );     *cpu=New Processor (thread->socket);    Connect (thread-socket,signal (Readyread)), Cpu,slot (Work ()));    Connect (thread, SIGNAL (finished ()), Thread, SLOT (Deletelater ()));    CPU--movetothread (thread);    Thread-Start ();}

There are a few strange things in this function.

The Socketthread class derives from Qthread, and the child threads are not interpreted.

The processor class derives from the Qobject, and this class is the focus.

The biggest problem with Qt multithreading is that while the run function of a child thread is running in a sub-thread, the other functions of the thread (including the signal/slot) are running in the main thread.

Our server is sure to process client requests, which are data requests for sockets, which are encapsulated in the readyread signal of the socket in Qt.

Even if you bind the Readyread signal in the Run function, the final signal will be triggered in the main thread.

The solution is to write a separate processing class, here is the processor class , movethread the child thread into this object, so that all the functions of this object are executed in the child thread, the workfunction Used by the server to accept requests and to return database data.

This is the QT 4.7, the official recommendation, because n multi-threaded writing is fundamentally wrong, the official can not endure.

② again see Socketthread class

classSocketthread: Publicqthread{Q_object Public: Socketthread (intsocketdescriptor,qobject*parent); intSocketdescriptor; Qtcpsocket*socket; voidrun ();}; Socketthread::socketthread (intId,qobject *parent): Qthread () {Socketdescriptor=ID; Socket=NewQtcpsocket;}voidSocketthread::run () {socket-Setsocketdescriptor (Socketdescriptor); Qthread::run ();}
Thread

The constructor does not have to say much about the ID of the socket that the incoming system assigns to the server.

The key is the virtual function run of Qthread. First, set the server's socket identification ID.

Remember to call  qthread::run (); Otherwise the run function is not fully executed.

③ again see Processor class

void processor::work () {    //qdebug () << "Current thread:" <<qthread::currentthreadid () < <endl;    Buff=m_socket->ReadAll ();    M_socket, write (buff);     // qdebug () <<buff<<endl;}
Processor

The member M_socket a pointer to save the socket address of the child thread.

And a qbytearray as a buffer buff.

ReadAll reads the client's Socket,write and writes back to the client's sokcet.

If you are bored, you can take the comment off and see if the work thread of the working function is different from the main one.

④client End

voidClient::send () {socket->connecttohost (Qhostaddress (Address->text ()),7399); QString x="2333, we're screwed."+qstring::number (cnt++); Socket-Write (X.tostdstring (). C_STR ());}voidClient::Get() {QString data=socket->ReadAll (); Qdebug ()<<"Receiving end:"<<data<<Endl; Socket-disconnectfromhost ();}

The strategies used here are as follows:

Each time a request is made, the server is connected once, and after receiving the server's reply, the connection is disconnected to prevent sever resources from being consumed.

The Get function acts as a slot function for the client's readyread.

QT builds multi-threaded server

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.