Qt實(shí)現(xiàn)TCP同步與異步讀寫(xiě)消息的示例代碼
一、異步讀寫(xiě)
在 Qt 中實(shí)現(xiàn) TCP 客戶(hù)端和服務(wù)器的同步和異步讀寫(xiě)消息涉及使用 QTcpSocket 和 QTcpServer 類(lèi)。這兩個(gè)類(lèi)提供了用于建立 TCP 連接、發(fā)送和接收數(shù)據(jù)的功能。下面是一個(gè)簡(jiǎn)單的示例,演示了如何在 Qt 中實(shí)現(xiàn) TCP 客戶(hù)端和服務(wù)器的同步和異步讀寫(xiě)消息:
TCP 服務(wù)器端示例(異步)
#include <QCoreApplication> #include <QTcpServer> #include <QTcpSocket> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QTcpServer server; server.listen(QHostAddress::Any, 1234); // 監(jiān)聽(tīng)端口 1234 QObject::connect(&server, &QTcpServer::newConnection, [&](){ QTcpSocket* clientSocket = server.nextPendingConnection(); QObject::connect(clientSocket, &QTcpSocket::readyRead, [&](){ QByteArray requestData = clientSocket->readAll(); qDebug() << "Received request from client:" << requestData; // Echo back the received data clientSocket->write(requestData); }); }); qDebug() << "TCP server started. Listening on port 1234..."; return a.exec(); }
TCP 客戶(hù)端端示例(異步)
#include <QCoreApplication> #include <QTcpSocket> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QTcpSocket client; client.connectToHost("127.0.0.1", 1234); // 連接到服務(wù)器的 IP 地址和端口 QObject::connect(&client, &QTcpSocket::connected, [&](){ qDebug() << "Connected to server."; // Send a message to the server client.write("Hello, Server!"); }); QObject::connect(&client, &QTcpSocket::readyRead, [&](){ QByteArray responseData = client.readAll(); qDebug() << "Received response from server:" << responseData; }); return a.exec(); }
示例簡(jiǎn)單演示了如何在 Qt 中實(shí)現(xiàn) TCP 客戶(hù)端和服務(wù)器的異步讀寫(xiě)消息。在異步通信中,使用信號(hào)和槽機(jī)制來(lái)處理數(shù)據(jù)的接收和發(fā)送。
二、同步讀寫(xiě)
以下是一個(gè)簡(jiǎn)單的示例,演示了如何在 Qt 中實(shí)現(xiàn) TCP 客戶(hù)端和服務(wù)器的同步讀寫(xiě)消息:
TCP 服務(wù)器端示例(同步)
#include <QCoreApplication> #include <QTcpServer> #include <QTcpSocket> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QTcpServer server; server.listen(QHostAddress::Any, 1234); // 監(jiān)聽(tīng)端口 1234 if (!server.isListening()) { qDebug() << "Failed to start TCP server."; return 1; } QTcpSocket* clientSocket = nullptr; while (true) { if (server.hasPendingConnections()) { clientSocket = server.nextPendingConnection(); break; } } if (clientSocket) { qDebug() << "Client connected."; // Read data from client QByteArray requestData = clientSocket->readAll(); qDebug() << "Received request from client:" << requestData; // Echo back the received data clientSocket->write(requestData); } return a.exec(); }
TCP 客戶(hù)端端示例(同步)
#include <QCoreApplication> #include <QTcpSocket> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QTcpSocket client; client.connectToHost("127.0.0.1", 1234); // 連接到服務(wù)器的 IP 地址和端口 if (!client.waitForConnected()) { qDebug() << "Failed to connect to server."; return 1; } qDebug() << "Connected to server."; // Send a message to the server client.write("Hello, Server!"); if (!client.waitForBytesWritten()) { qDebug() << "Failed to write data to server."; return 1; } if (!client.waitForReadyRead()) { qDebug() << "Failed to read response from server."; return 1; } // Read response from server QByteArray responseData = client.readAll(); qDebug() << "Received response from server:" << responseData; return a.exec(); }
示例簡(jiǎn)單演示了如何在 Qt 中實(shí)現(xiàn) TCP 客戶(hù)端和服務(wù)器的同步讀寫(xiě)消息。在同步通信中,使用 waitForConnected、waitForBytesWritten 和 waitForReadyRead 等方法來(lái)等待連接建立、數(shù)據(jù)寫(xiě)入和數(shù)據(jù)讀取完成。這種方式適用于需要確保數(shù)據(jù)傳輸完整性和順序性的場(chǎng)景。
到此這篇關(guān)于Qt實(shí)現(xiàn)TCP同步與異步讀寫(xiě)消息的示例代碼的文章就介紹到這了,更多相關(guān)Qt TCP同步與異步讀寫(xiě)消息內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)企業(yè)員工管理系統(tǒng)開(kāi)發(fā)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)企業(yè)員工管理系統(tǒng)開(kāi)發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08C語(yǔ)言關(guān)鍵字auto與register及static專(zhuān)項(xiàng)詳解
這篇文章主要解釋了c語(yǔ)言中什么是數(shù)據(jù)類(lèi)型,什么是變量,他們的真正含義是什么。分析了屬性關(guān)鍵字auto,register和static的用法2022-07-07C++實(shí)現(xiàn)list增刪查改模擬的示例代碼
本文主要介紹了C++實(shí)現(xiàn)list增刪查改模擬,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12C++實(shí)現(xiàn)簡(jiǎn)單的圖書(shū)管理系統(tǒng)
本文給大家分享的是使用C++實(shí)現(xiàn)簡(jiǎn)單的圖書(shū)管理系統(tǒng)的代碼,本系統(tǒng)采用了面向?qū)ο蟮某绦蛟O(shè)計(jì)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-08-08C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的貪吃蛇小游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的貪吃蛇小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06詳解C語(yǔ)言中index()函數(shù)和rindex()函數(shù)的用法
這篇文章主要介紹了C語(yǔ)言中index()函數(shù)和rndex()函數(shù)的用法,是C語(yǔ)言入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),要的朋友可以參考下2015-08-08