Qtcpserver Multithreading Implementation

Source: Internet
Author: User
Tags socket thread

Transferred from: http://www.dushibaiyu.com/2013/12/qtcpserver%E5%A4%9A%E7%BA%BF%E7%A8%8B%E5%AE%9E%E7%8E%B0.html

When implemented, each inherits Qtcpserver and Qtcpscoket to implement its own classes.

Inheritance Qtcpserver allocates threads for each client connection and accepts signals and slots that process Tcpscoket, sends messages, stores connection information, and so on.

Inherit Qtcpscoket to process the communication data and increase the signal parameters, in order to better cooperate with the TCPServer.

The first is to inherit and rewrite Qtcpserver's incomingconnection function to implement Tcpsocket connection creation and allocation.

The default description for the document is:

This virtual function was called by Qtcpserver if a new connection is available. The Socketdescriptor argument is the native socket descriptor for the accepted connection.

The base implementation creates a qtcpsocket, sets the socket descriptor and then stores the qtcpsocket in an internal LIS T of pending connections. Finally newconnection () is emitted.

Reimplement this function to alter the server's behavior when a connection is available.

If This server is a using qnetworkproxy then the socketdescriptor may isn't being usable with native socket functions, and should Only being used with Qtcpsocket::setsocketdescriptor ().

Note:if you want to handle an incoming connection as a new Qtcpsocket object in another thread you had to pass the Socke Tdescriptor to the other thread and create the Qtcpsocket object there and use its Setsocketdescriptor () method.

Translations (Google Translate and your own simple corrections):

This virtual function is called when the Qtcpserver has a new connection. The Socketdescriptor parameter is the local socket descriptor that is used to accept the connection.

The function creates a qtcpsocket, sets the socket descriptor to socketdescriptor, and then stores the Qtcpsocket internal manifest in the pending connection. The last Newconnection () was fired.

Re-implement this function to change the behavior of the server when a connection is available.

If the server uses qnetworkproxy then Socketdescriptor may not work with the native socket function and can only be used in Qtcpsocket:: Setsocketdescriptor ().

Note: If you want to handle incoming connections on a new Qtcpsocket object in another thread, you must pass socketdescriptor to the other thread and create a Qtcpsocket object that exists and uses its Setsocketdescriptor () method.

So we have to rewrite this function first:

The following is a copy of the Qtcpserver's own class declaration, code comments personally think quite detailed:

01 A server that inherits Qtcpserver to implement multithreaded Tcpscoket.
02 Class Mytcpserver:public Qtcpserver
03 {
04 Q_object
05

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.