Qt實(shí)現(xiàn)TCP同步與異步讀寫消息的示例代碼
一、異步讀寫
在 Qt 中實(shí)現(xiàn) TCP 客戶端和服務(wù)器的同步和異步讀寫消息涉及使用 QTcpSocket 和 QTcpServer 類。這兩個(gè)類提供了用于建立 TCP 連接、發(fā)送和接收數(shù)據(jù)的功能。下面是一個(gè)簡單的示例,演示了如何在 Qt 中實(shí)現(xiàn) TCP 客戶端和服務(wù)器的同步和異步讀寫消息:
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)聽端口 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 客戶端端示例(異步)
#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(); }
示例簡單演示了如何在 Qt 中實(shí)現(xiàn) TCP 客戶端和服務(wù)器的異步讀寫消息。在異步通信中,使用信號和槽機(jī)制來處理數(shù)據(jù)的接收和發(fā)送。
二、同步讀寫
以下是一個(gè)簡單的示例,演示了如何在 Qt 中實(shí)現(xiàn) TCP 客戶端和服務(wù)器的同步讀寫消息:
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)聽端口 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 客戶端端示例(同步)
#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(); }
示例簡單演示了如何在 Qt 中實(shí)現(xiàn) TCP 客戶端和服務(wù)器的同步讀寫消息。在同步通信中,使用 waitForConnected、waitForBytesWritten 和 waitForReadyRead 等方法來等待連接建立、數(shù)據(jù)寫入和數(shù)據(jù)讀取完成。這種方式適用于需要確保數(shù)據(jù)傳輸完整性和順序性的場景。
到此這篇關(guān)于Qt實(shí)現(xiàn)TCP同步與異步讀寫消息的示例代碼的文章就介紹到這了,更多相關(guān)Qt TCP同步與異步讀寫消息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言實(shí)現(xiàn)企業(yè)員工管理系統(tǒng)開發(fā)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)企業(yè)員工管理系統(tǒng)開發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08C語言關(guān)鍵字auto與register及static專項(xiàng)詳解
這篇文章主要解釋了c語言中什么是數(shù)據(jù)類型,什么是變量,他們的真正含義是什么。分析了屬性關(guān)鍵字auto,register和static的用法2022-07-07C++實(shí)現(xiàn)list增刪查改模擬的示例代碼
本文主要介紹了C++實(shí)現(xiàn)list增刪查改模擬,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12C++實(shí)現(xiàn)簡單的圖書管理系統(tǒng)
本文給大家分享的是使用C++實(shí)現(xiàn)簡單的圖書管理系統(tǒng)的代碼,本系統(tǒng)采用了面向?qū)ο蟮某绦蛟O(shè)計(jì)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-08-08詳解C語言中index()函數(shù)和rindex()函數(shù)的用法
這篇文章主要介紹了C語言中index()函數(shù)和rndex()函數(shù)的用法,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識,要的朋友可以參考下2015-08-08