Qtcpsocket Server timer, multi-threaded combination

Source: Internet
Author: User

Qtcpsocket when writing a server program, it is necessary to periodically send monitoring data to the client while responding to client requests.
The problem is: Qtcpsocket can only be used in the same thread, and the timer will run in the main threads
Solution: Send a signal to the encapsulated client data processing socket in the timer and connect the signal to the operation in the socket in the thread.
Clientsocket.h#ifndef clientsocket_h#define clientsocket_h#include <QTcpSocket> #include <QTimer> Class Clientsocket:public Qtcpsocket{q_objectpublic:clientsocket (Qobject *parent); ~clientsocket (); signals:void Sigreaddata (const QString &data);p ublic slots:void slottimeout (); void Slotreadyread (); void Slotwritedata (const qstring& data);p rivate:qtimer* _timer;}; #endif//clientsocket_h//clientsocket.cpp#include "Clientsocket.h" #include <qdebug>clientsocket:: Clientsocket (Qobject *parent): Qtcpsocket (parent) {Connect (this, &qabstractsocket::readyread, this, & Clientsocket::slotreadyread);//_timer = new Qtimer (this);//connect (_timer, &qtimer::timeout, this, & Clientsocket::slottimeout);//_timer->start (2000);} Clientsocket::~clientsocket () {}void clientsocket::slotreadyread () {QString recv = ReadAll (); Emit sigreaddata (recv);} void Clientsocket::slottimeout () {Write (Qbytearray (This->objectname (). tostdstring (). C_STR ()));} void Clientsocket::slotwritedata(const qstring& data) {Write (Data.tostdstring (). C_STR ()); Flush ();} Serverlistener.h#ifndef serverlistener_h#define serverlistener_h#include <QTcpServer> #include <qtimer > #include "clientsocket.h" Class Serverlistener:public Qtcpserver{q_objectpublic:serverlistener (Qobject *parent) ; ~serverlistener (); virtual void incomingconnection (qintptr handle); signals:void sigwritedata (const qstring& Data );p ublic slots:void slotreaddata (const qstring& data), void Slotsendmmidatatimeout ();p rivate:clientsocket *_ Clientsock; Qtimer* _timer;}; #endif//Serverlistener_h//serverlistener.cpp#include "Serverlistener.h" #include <QThread> #include < Qdebug>serverlistener::serverlistener (Qobject *parent): Qtcpserver (parent) {}serverlistener::~serverlistener () {}void serverlistener::incomingconnection (qintptr handle) {_clientsock = new Clientsocket (0x00);_clientsock-> Setsocketdescriptor (handle); Qthread *thread = new Qthread (this); _clientsock->movetothread (thread); _timer = new QTimer (This): Connect (_clientsock, &clientsocket::sigreaddata, this, &serverlistener::slotreaddata); connect (This, &serverlistener::sigwritedata, _clientsock, &clientsocket::slotwritedata); Connect (_timer, & Qtimer::timeout, this, &serverlistener::slotsendmmidatatimeout); _timer->start (+); Thread->start ();} void Serverlistener::slotreaddata (const qstring& data) {qdebug () << data;} void Serverlistener::slotsendmmidatatimeout () {QString data = qstring::fromlocal8bit ("Ryan data");//_clientsock-> Write (Data.tostdstring (). C_STR ()); If you are here, you will get an error emit serverlistener::sigwritedata (data);} Main.cpp#include "Serverlistener.h" #include <QtCore/QCoreApplication> #include <qhostaddress>int main (int argc, char *argv[]) {Qcoreapplication A (argc, argv); Serverlistener Listener (0x00); Listener.listen (Qhostaddress::any, 6000); return a.exec ();}


Qtcpsocket Server timer, multi-threaded combination

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.