what is the best way to make a network app? multi-thread...heart-beating...
-
what is the best way to make a network app? multi-thread...heart-beating...
i seems that there is no msg loop explicitly in main()...
i want to read data from the server in a loop.is there any good example to create a thread and do this network thing(eg. heart-beating)?
thank you!
-
what is the best way to make a network app? multi-thread...heart-beating...
i seems that there is no msg loop explicitly in main()...
i want to read data from the server in a loop.is there any good example to create a thread and do this network thing(eg. heart-beating)?
thank you!
@opengpu2 said:
what is the best way to make a network app? multi-thread...heart-beating...
The right answer is: "There isn't the BEST way". It depends of your application feauters and requirements
i seems that there is no msg loop explicitly in main()...
i want to read data from the server in a loop.Qt usually prefers the Asynchronous approach but you can read from a socket with blocking API like
QUdpSocket::readDatagram()is there any good example to create a thread and do this network thing(eg. heart-beating)?
thank you!
-
yes, i don't want to use boost, and i want to use Qt.
what is the API or demo that shows how to create a thread in Qt?
and where should i create the timer? in the main thread or the new thread?
and how to write the slot...thank you.
and i think this timer method is sort of heart-beating, right? -
Hi,
as I said before you don't necessarily need a Thread for this work; Do you need to receive data from server or request data to the server?
- Receive Data (f.i. using a UDP socket)
void ServerConnection::initSocket() { udpSocket = new QUdpSocket(this); udpSocket->bind(QHostAddress::LocalHost, 7755); connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())); } void ServerConnection::readPendingDatagrams() { while (udpSocket->hasPendingDatagrams()) { QByteArray datagram; datagram.resize(udpSocket->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort); processTheDatagram(datagram); } }- Request Data
myTimer = new QTimer(this); connect(myTimer, &QTimer::timeout, this, &MyClass::requestData); void MyClass::requestData() { // Request data to the server } -
thank you very much.
i dont use QUdpSocket..i use my own network classes.
right now i Tick the network io in the new thread. that's why i want to readData in msg loop, or heart-beating...maybe Tick the network io in the new thread, and use a timer as heart-beating in the main thread to readData() is a good method?
-
Hi,
I think you should use something only if you really need it.
Qt is designed to work in asynchronous mode so you can handle multiple source of data in a single-thread application.You should move to multi-threading when you need the handle long time processing or when you need to non block who's sending you data.