Qt中TCP服务端编程
生活随笔
收集整理的這篇文章主要介紹了
Qt中TCP服务端编程
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 1 Qt中的TCP服務(wù)端編程
- 1.1 TCP服務(wù)端編程介紹
- 1.2 Qt中的TCP服務(wù)端編程
1 Qt中的TCP服務(wù)端編程
1.1 TCP服務(wù)端編程介紹
網(wǎng)絡(luò)中的服務(wù)端:
- 服務(wù)端是為客戶端服務(wù)的,服務(wù)的內(nèi)容諸如向客戶端提供資源,保存客戶端數(shù)據(jù),為客戶端提供功能接口等。
Client/Server軟件架構(gòu)簡介:
Client/Server軟件架構(gòu)的特點(diǎn):
- 服務(wù)端被動(dòng)接受連接(服務(wù)端無法主動(dòng)連接客戶端)。
- 服務(wù)端必須公開網(wǎng)絡(luò)地址(容易受到攻擊)。
- 在職責(zé)上:
- 客戶端傾向于處理用戶交互及體驗(yàn)(GUI)。
- 服務(wù)端傾向于用戶數(shù)據(jù)的組織和存儲(數(shù)據(jù)處理)。
Browser/Server軟件架構(gòu)簡介(B/S):
- B/S是一種特殊的C/S網(wǎng)絡(luò)架構(gòu)。
- B/S中的客戶端統(tǒng)一使用瀏覽器(Browser)。
- B/S中的客戶端GUI通常采用HTML進(jìn)行開發(fā)。
- B/S中的客戶端與服務(wù)端通常采用http協(xié)議進(jìn)行通信。
1.2 Qt中的TCP服務(wù)端編程
Qt中的TCP服務(wù)端編程:
- Qt提供了QTcpServer類(封裝了TCP協(xié)議細(xì)節(jié))。
- 將QTcpServer的對象當(dāng)作黑盒使用,進(jìn)行連接監(jiān)聽。
- 每一個(gè)連接生成一個(gè)QTcpSocket對象進(jìn)行通信。
QTcpServer的使用方式:
QTcpServer的注意事項(xiàng):
- 用于處理客戶端連接,不進(jìn)行具體通信。
- 監(jiān)聽的端口只用于響應(yīng)連接請求。
- 監(jiān)聽到連接后,生成QTcpSocket對象與客戶端通信。
Client/Server交互流程:
代碼實(shí)現(xiàn)如下:
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(); }參考資料:
總結(jié)
以上是生活随笔為你收集整理的Qt中TCP服务端编程的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 同一只基金为什么还有AC之分 满足不同
- 下一篇: 实现虚拟磁盘驱动