網站首頁 編程語言 正文
本文實例為大家分享了Qt實現TCP網絡編程的具體代碼,供大家參考,具體內容如下
1.Qt中的TCP客戶端編程
Qt中的TCP客戶端編程:
對于Qt編程而言,網絡只是數據傳輸的通道
Qt提供了QTcpSocket類(封裝了TCP協議細節)
將QTcpSocket的對象當做黑盒使用,進行數據收發
QTcpSocket的使用方式:
1.連接服務端主機(connectToHost())
2.發送數據/接受數據(write()/read())
3.關閉連接(close())
QTcpSocket的注意事項:
默認情況下,QTcpSocket使用異步編程的方式:
操作完成后立即返回
通過發送信號的方式返回操作結果
QTcpSocket提供了輔助函數,可完成同步編程的方式
waitForConnected()/waitForDisconnected()
waitForBytesWritten()/waitForReadyread()
QTcpSocket的同步編程:
編程實驗:同步編程
#include <QCoreApplication>
#include <QTcpSocket>
#include <QDebug>
#include <QThread>
void SyncClientDemo()
{
? ? QTcpSocket client;
? ? char buf[256] = {0};
? ? client.connectToHost("127.0.0.1",8080);
? ? qDebug() << "Connected:" << client.waitForConnected();
? ? qDebug() << "Send Bytes:" << client.write("CKY");
? ? qDebug() << "Send Status:" << client.waitForBytesWritten();
? ? qDebug() << "Data Avilable:" << client.waitForReadyRead();
? ? qDebug() << "Received Bytes:" << client.read(buf, sizeof(buf));
? ? qDebug() << "Received Data:" << buf;
? ? QThread::sleep(5000);
? ? client.close();
? ? client.waitForDisconnected();
}
int main(int argc, char *argv[])
{
? ? QCoreApplication a(argc, argv);
? ? SyncClientDemo();
? ? return a.exec();
}
QTcpSocket的異步編程:
QTcpSocket對象通過發送信號的方式返回操作結果
可以在程序中將對應的信號連接到槽函數,獲取結果
在GUI應用程序中通常使用QTcpSocket的異步方式
QTcpSocket中的關鍵信號:
connected():成功連接遠端主機
disconnected():遠端主機斷開連接
readyRead():遠程數據到達本機
bytesWritten(qint64):數據成功發送至系統(OS)
編程實驗:QTcpSocket異步編程
#include "clientdemo.h"
#include <QDebug>
#include <QHostAddress>
ClientDemo::ClientDemo(QObject* parent) : QObject(parent)
{
? ? connect(&m_client, SIGNAL(connected()), this, SLOT(onConnected()));
? ? connect(&m_client, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
? ? connect(&m_client, SIGNAL(readyRead()), this, SLOT(onDataReady()));
? ? connect(&m_client, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));
}
void ClientDemo::onConnected()
{
? ? qDebug() << "onConnected()";
? ? qDebug() << "Local Address:" << m_client.localAddress();
? ? qDebug() << "Loacl Port:" << m_client.localPort();
}
void ClientDemo::onDisconnected()
{
? ? qDebug() << "onDiecennected()";
}
void ClientDemo::onDataReady()
{
? ? char buf[256] = {0};
? ? qDebug() << "onDataReady:" << m_client.read(buf, sizeof(buf));
? ? qDebug() << "Data:" << buf;
}
void ClientDemo::onBytesWritten(qint64 bytes)
{
? ? qDebug() << "onBytesWritten" << bytes;
}
void ClientDemo::connectTo(QString ip, int port)
{
? ? m_client.connectToHost(ip, port);
}
qint64 ClientDemo::send(const char* data, int len)
{
? ? return m_client.write(data, len);
}
qint64 ClientDemo::available()
{
? ? return m_client.bytesAvailable();
}
void ClientDemo::close()
{
? ? m_client.close();
}
1.Qt中的TCP服務端編程
網絡中的服務端:
服務端是為客戶端服務的,服務的內容諸如向客戶端提供資源,保存客戶端數據,為客戶端提供功能接口,等
Client/Server軟件架構簡介
特點;
服務端被動接受連接(服務端無法主動連接客戶端)
服務端必須公開網絡地址(容易受到攻擊)
在職責上:
客戶端傾向于處理用于交互及體驗(GUI)
服務端傾向于用戶數據的組織和存儲(數據處理)
B/S網絡結構是什么?
Browser/Server軟件架構簡介
B/S是一種特殊的C/S網絡架構
B/S中的客戶端統一使用瀏覽器(Browser)
B/S中的客戶端GUI通常采用HTML進行開發
B/S中的客戶端與服務端通常采用http協議進行通信
Qt中的TCP服務端編程:
Qt提供了QTcpServer類
將QTcpServer的對象當做黑盒使用,進行連接監聽
每一個連接生成一個QTcpSocket對象進行通信
QTcpServer的使用方式:
監聽本機地址的端口(listen())
通過信號通知客戶端連接(newConnection())
獲取QTcpSocket通信對象(nextPendingConnection())
停止監聽(close())
QTcpServer的注意事項:
用于處理客戶端連接,不進行具體通信
監聽的端口只用于響應連接請求
監聽到連接后,生成QTcpSocket對象與客戶端通信
Client/Server交互流程:
編程實驗:QServerSocket編程
#include "serverdemo.h"
#include "QHostAddress"
#include <QDebug>
#include <QTcpServer>
#include <QObjectList>
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)));
}
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();
? ? }
}
void ServerDemo::onConnected()
{
? ? QTcpServer* tcp = dynamic_cast<QTcpServer*>(sender());
? ? if(tcp != NULL)
? ? {
? ? ? ? qDebug() << "onConnected()";
? ? ? ? qDebug() << "Local Address:" << tcp->localAddress();
? ? ? ? qDebug() << "Loacl Port:" << tcp->localPort();
? ? }
}
void ServerDemo::onDisconnected()
{
? ? qDebug() << "onDiecennected()";
}
void ServerDemo::onDataReady()
{
? ? QTcpServer* tcp = dynamic_cast<QTcpServer*>(sender());
? ? char buf[256] = {0};
? ? if(tcp != NULL)
? ? {
? ? ? ? qDebug() << "onDataReady:" << tcp->read(buf, sizeof(buf));
? ? ? ? qDebug() << "Data:" << buf;
? ? }
}
void ServerDemo::onBytesWritten(qint64 bytes)
{
? ? qDebug() << "onBytesWritten" << bytes;
}
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();
? ? ? ? }
? ? }
}
原文鏈接:https://blog.csdn.net/weixin_46571142/article/details/123029759
相關推薦
- 2023-01-21 VmWare安裝Centos后配置Net網絡SSH鏈接問題及解決_VMware
- 2023-02-07 基于C#實現磁性吸附窗體_C#教程
- 2022-03-10 Winform自定義控件在界面拖動、滾動鼠標時閃爍的解決方法_C#教程
- 2022-05-31 Android實現文件資源管理器雛形_Android
- 2023-01-05 Go單例模式與Once源碼實現_Golang
- 2022-09-10 Python自動打印被調用函數變量名及對應值?_python
- 2022-09-15 Kotlin創建一個好用的協程作用域_Android
- 2022-05-06 Spring Boot 使用 RequiredArgsConstructor 參數 onConstr
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支