C++ Qt 68 - QTcpServer using multiple threads

VoidRealms · 71K views · 2011 · 27:14

Open on YouTube

C, C++, Rust, Go & systems languagesNetworking & internet infrastructureSoftware architecture & designtutorial

Why Stack Overflow recommends it

Stack Overflow users link it as a worked example of a multithreaded QTcpServer, both to answer questions about Qt servers and as a starting point for their own threaded server code.

Recommended by 2 people across 2 different months, 2013 to 2018.

What it is

A 27-minute episode from VoidRealms' C++ Qt tutorial series showing how to build a QTcpServer that hands each incoming connection to its own thread. It walks through overriding incomingConnection, passing the socket descriptor to a worker thread, and handling reads and disconnects there.

What people said

Recommends Gediminas · 2013-05 · Stack Overflow · on “Qt portable IPC: only QSharedMemory?”

You can use QLocalSocket [LINK] and QlocalServer [LINK] for IPC too. Those two classes are really easy to use and they can be implemented ~almost the same way as you would using Qt TCP socket client/server. You can take a look at some of these youtube videos: QTCPServer - a basic TCP server application [LINK] QTcpServer using multiple threads [this video] (Almost the sames principles would apply for "Local" server/client instead of "TCP")

Recommends Mohammad Kanan · 2018-01 · Stack Overflow · on “How to send messages to connected clients from server in Qt”

You need to track your clients .. I only know "socketDescriptor" .. if you want to communicate down your clients, try to send the message to each client socket through its socket Descriptor .. a solid example of reliable QTcpServer is found here [this video]

Cites proton · 2016-05 · Stack Overflow · on “passing information on Threaded TcpServer from one thread to another”

I tried to create simple server like in link 1. [this video] void Test_Server::incomingConnection(int socketDescriptor_) { qDebug() << socketDescriptor_ << "connecting..."; Test_Thread *thread_ = new Test_Thread(number_,socketDescriptor_,this); connect(thread_,SIGNAL(finished()),thread_,SLOT(deleteLater())); thread_->start(); number_++; } //// void Test_Thread::run() { qDebug() << this->Socket_Descriptor_ << "starting thread"; socket = new QTcpSocket(); if(!socket->setSocketDescriptor(Socket_Descriptor_)) { qDebug() << "ERROR"; } connect(socket,SIGNAL(readyRead()),this,SLOT(Ready_read_()),Qt::DirectConnection); connect(socket,SIGNAL(disconnected()),this,SLOT(disconnected_()),Qt::DirectConnection)