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 |