Qt中TCP服务端编程
生活随笔
收集整理的這篇文章主要介紹了
Qt中TCP服务端编程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1 Qt中的TCP服務端編程
- 1.1 TCP服務端編程介紹
- 1.2 Qt中的TCP服務端編程
1 Qt中的TCP服務端編程
1.1 TCP服務端編程介紹
網絡中的服務端:
- 服務端是為客戶端服務的,服務的內容諸如向客戶端提供資源,保存客戶端數據,為客戶端提供功能接口等。
Client/Server軟件架構簡介:
Client/Server軟件架構的特點:
- 服務端被動接受連接(服務端無法主動連接客戶端)。
- 服務端必須公開網絡地址(容易受到攻擊)。
- 在職責上:
- 客戶端傾向于處理用戶交互及體驗(GUI)。
- 服務端傾向于用戶數據的組織和存儲(數據處理)。
Browser/Server軟件架構簡介(B/S):
- B/S是一種特殊的C/S網絡架構。
- B/S中的客戶端統一使用瀏覽器(Browser)。
- B/S中的客戶端GUI通常采用HTML進行開發。
- B/S中的客戶端與服務端通常采用http協議進行通信。
1.2 Qt中的TCP服務端編程
Qt中的TCP服務端編程:
- Qt提供了QTcpServer類(封裝了TCP協議細節)。
- 將QTcpServer的對象當作黑盒使用,進行連接監聽。
- 每一個連接生成一個QTcpSocket對象進行通信。
QTcpServer的使用方式:
QTcpServer的注意事項:
- 用于處理客戶端連接,不進行具體通信。
- 監聽的端口只用于響應連接請求。
- 監聽到連接后,生成QTcpSocket對象與客戶端通信。
Client/Server交互流程:
代碼實現如下:
ServerDemo.h:
#ifndef SERVERDEMO_H #define SERVERDEMO_H#include <QObject> #include <QTcpServer>class ServerDemo : public QObject {Q_OBJECTQTcpServer m_server;public:ServerDemo(QObject* parent = NULL);bool start(int port);void stop();~ServerDemo();protected slots:void onNewConnection();void onConnected();void onDisconnected();void onDataReady();void onBytesWritten(qint64 bytes); };#endif // SERVERDEMO_HServerDemo.cpp:
#include "ServerDemo.h" #include <QHostAddress> #include <QTcpSocket> #include <QObjectList> #include <QDebug>ServerDemo::ServerDemo(QObject* parent) : QObject(parent) {connect(&m_server, SIGNAL(newConnection()), this, SLOT(onNewConnection())); }void ServerDemo::onNewConnection() {qDebug() << "onNewConnection";QTcpSocket* tcp = m_server.nextPendingConnection();connect(tcp, SIGNAL(connected()), this, SLOT(onConnected()));connect(tcp, SIGNAL(disconnected()), this, SLOT(onDisconnected()));connect(tcp, SIGNAL(readyRead()), this, SLOT(onDataReady()));connect(tcp, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64))); }void ServerDemo::onConnected() {QTcpSocket* tcp = dynamic_cast<QTcpSocket*>(sender());if( tcp != NULL ){qDebug() << "onConnected";qDebug() << "Local Address:" << tcp->localAddress();qDebug() << "Local Port:" << tcp->localPort();} }void ServerDemo::onDisconnected() {qDebug() << "onDisconnected"; }void ServerDemo::onDataReady() {QTcpSocket* tcp = dynamic_cast<QTcpSocket*>(sender());char buf[256] = {0};if( tcp != NULL ){qDebug() << "onDataReady:" << tcp->read(buf, sizeof(buf)-1);qDebug() << "Data:" << buf;} }void ServerDemo::onBytesWritten(qint64 bytes) {qDebug() << "onBytesWritten:" << bytes; }bool ServerDemo::start(int port) {bool ret = true;if( !m_server.isListening() ){ret = m_server.listen(QHostAddress("127.0.0.1"), port);}return ret; }void ServerDemo::stop() {if( m_server.isListening() ){m_server.close();} }ServerDemo::~ServerDemo() {const QObjectList& list = m_server.children();for(int i=0; i<list.length(); i++){QTcpSocket* tcp = dynamic_cast<QTcpSocket*>(list[i]);if( tcp != NULL ){tcp->close();}} }main.cpp:
#include <QCoreApplication> #include <QTcpSocket> #include <QDebug> #include <QThread> #include "ClientDemo.h" #include "ServerDemo.h"void SyncClientDemo() {QTcpSocket client;char buf[256] = {0};client.connectToHost("127.0.0.1", 8080);qDebug() << "Connected:" << client.waitForConnected();qDebug() << "Send Bytes:" << client.write("D.T.Software");qDebug() << "Send Status:" << client.waitForBytesWritten();qDebug() << "Data Available:" << client.waitForReadyRead();qDebug() << "Received Bytes:" << client.read(buf, sizeof(buf)-1);qDebug() << "Received Data:" << buf;client.close();// client.waitForDisconnected(); }int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);ServerDemo server;server.start(8080);return a.exec(); }參考資料:
總結
以上是生活随笔為你收集整理的Qt中TCP服务端编程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 同一只基金为什么还有AC之分 满足不同
- 下一篇: 实现虚拟磁盘驱动