Measure the test taker's knowledge about how to build a multi-thread server in QT 4.

Source: Internet
Author: User

Address: http://mobile.51cto.com/symbian-272641.htm

 

Http://www.cnblogs.com/huipengkankan/archive/2012/05/27/2519747.html

 

First, let's talk aboutMultithreadingThe process of understanding this term. I have heard of it many times beforeMultithreadingThis word is often usedServerThereforeMultithreadingMisunderstanding:ServerUnique feature; After carefully studying the course designMultithreadingTo understand the true meaning. Simply put, multiple threads run together at the same time, and different threads can execute different operations. For example, an image processing tool can use the mouse to move the image while using the shortcut key to scale the image. At this time, moving the image and scaling the image are processed by different threads. If notMultithreadingIt is a single thread, so only one operation can be performed.

ForServerFor example,MultithreadingThis feature is too useful becauseMultithreadingMakeServerIt may respond to requests from multiple clients at the same time.ServerMostly usedMultithreadingSo it will lead to my initial misunderstanding.

No matterMultithreading, OrServer,QtHas encapsulated a specific class, so it is very convenient to use. The following is a server that supports multiple threads and TCP.

First, create a server. Create a new class (server) that inherits the qtcpserver class in QT. The server is responsible for listening to ports. When a client tries to establish a connection with the server, allocate a socket to connect to the client before data communication. The listen () method of qtcpserver executes the listening process. You can specify the listening address and port. If the qhostaddress type listening address is specified, the address is listened to. Otherwise, all addresses are listened to. If the quint16 type listening port is specified, the listening port is listened to. Otherwise, randomly select a listening port.

 
 
  1. view plaincopy to clipboardprint?  
  2. Server * server = new Server;      
  3. if(!server->listen(host,port)){      
  4. ...//error      
  5. }    

Server * Server = new server; If (! Server-> listen (host, Port )){... // error} qtcpserver has a virtual function incomingconnection (INT socketdescriptor). The server automatically calls this function whenever listening to a client trying to establish a connection. Therefore, when processing this request, you can redefine the incomingconnection () function in the movie function, that is, in the definition stage of the sub-server.

For a multi-threaded server, whenever the client tries to connect, the server should start a thread to serve the client. Therefore, incomingconnection () all you need to do with this function is to create a thread, and the role of the created thread is to serve the client, and establishing a socket connection is the basis. When the server listens to the client and tries to establish a socket connection, it will allocate a unique socketdescriptor for the socket. This identifier will be used when the server establishes a socket connection, so it should be provided to every thread.

Use multiple threads in QT and create a class (thread) to inherit the qthread class. The qthread class also has a virtual function. This function is run (). After the thread is established and started (qthread: Start (), the code is executed. Therefore, the logical process of a thread should be defined in run. The server thread needs to establish a connection based on the socket identified by socketdescriptor, and then perform data communication. Therefore, it is necessary to pass socketdescriptor into the thread. As mentioned above, the thread is established in incomingconnection, use the constructor to pass socketdescriptor into the Thread class, and then use socketdescriptor to establish a socket connection.

Define incomingconnection ()

 
 
  1. View plaincopy to clipboardprint?
  2. Void incomingconnection (INT socketdescriptor ){
  3. Thread * thread = new thread (socketdescriptor );
  4. Thread-> Start ();
  5. }
  6. Void incomingconnection (INT socketdescriptor) {thread * thread = new thread (socketdescriptor); thread-> Start ();} defines run ()
  7. View plaincopy to clipboardprint?
  8. Void run (){
  9. Qtcpsocket * socket = new qtcpsocket (socketdescriptor );
  10. ... // Data Communication
  11. }
  12. Void run () {qtcpsocket * socket = new qtcpsocket (socketdescriptor);... // data communication} since then, a simple multi-threaded server has been established.

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.