網(wǎng)站首頁 編程語言 正文
本文實例為大家分享了QT實現(xiàn)簡單TCP通信的具體代碼,供大家參考,具體內(nèi)容如下
開發(fā)環(huán)境:win7
開發(fā)軟件:Qt5.5.0
下圖是實現(xiàn)效果
首先點擊客戶端的connect與服務器連接,連接成功后再服務器會顯示“成功連接”,然后在客戶端會顯示“成功與服務器建立連接”,然后我們就可以分別在服務器和客戶端向?qū)Ψ桨l(fā)送數(shù)據(jù)了。
1 TCP介紹
傳輸控制協(xié)議(TCP,Transmission Control Protocol)是一種面向連接的、可靠的、基于字節(jié)流的傳輸層通信協(xié)議。
在QT中的網(wǎng)絡編程的過程如下:服務器有兩個套接字,一個負責監(jiān)聽(QTcpServer),另一個負責通信(QTcpSocket),而客戶端只有一個負責通信的套接字(QTcpSocket)。
通信過程如下圖:
2 Tcp服務器實現(xiàn)
//監(jiān)聽套接字,指定父對象,讓其自動回收空間
tcpServer = new QTcpServer(this);
tcpServer->listen(QHostAddress::AnyIPv4, 6000);
當有新的連接建立時,在服務器界面接收區(qū)處顯示客戶端地址和端口,并且等待數(shù)據(jù)發(fā)送過來,接收并顯示:
connect(tcpServer, &QTcpServer::newConnection,
? ? ? ? ? ? [=](){
? ? ? ? ? ? ? ? //取出建立好連接的套接字
? ? ? ? ? ? ? ? //cout << "有新的客戶端進行連接";
? ? ? ? ? ? ? ? tcpSocket = tcpServer->nextPendingConnection();
? ? ? ? ? ? ? ? //獲取對方的IP和端口
? ? ? ? ? ? ? ? QString ip = tcpSocket->peerAddress().toString();
? ? ? ? ? ? ? ? quint16 port = tcpSocket->peerPort();
? ? ? ? ? ? ? ? QString temp = QString("[%1:%2]:成功連接").arg(ip).arg(port); ? ? ? ?
? ? ? ? ? ? ? ? ui->textEditRead->setText(temp);
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? connect(tcpSocket, &QTcpSocket::readyRead,
? ? ? ? ? ? ? ? ? ? ? ? [=](){
? ? ? ? ? ? ? ? ? ? ? ? ? ? //cout << "有數(shù)據(jù)從客戶端發(fā)過來";
? ? ? ? ? ? ? ? ? ? ? ? ? ? //從通信套接字取出內(nèi)容
? ? ? ? ? ? ? ? ? ? ? ? ? ? QByteArray array = tcpSocket->readAll();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ui->textEditRead->append(array);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? );
? ? ? ? ? ? }
? ? ? ? ? ? );
}
發(fā)送數(shù)據(jù):
void ServerWidget::on_buttonSend_clicked()
{
? ? if(NULL == tcpSocket){
? ? ? ? return;
? ? }
? ? //獲取編輯區(qū)內(nèi)容
? ? QString str = ui->textEditWrite->toPlainText();
? ? //給對方發(fā)送數(shù)據(jù)。使用套接字是tcpSocket
? ? tcpSocket->write(str.toUtf8().data());
}
點擊close按鈕,如果tcpSocket沒有分配內(nèi)存,則退出,否則斷開連接。
void ServerWidget::on_buttonClose_clicked()
{
? ? if(NULL == tcpSocket){
? ? ? ? return;
? ? }
? ? //主動和客戶端斷開連接
? ? tcpSocket->disconnectFromHost();
? ? tcpSocket->close();
? ? tcpSocket = NULL;
}
3 TCP客戶端實現(xiàn)
分配空間,指定父對象
tcpSocket = new QTcpSocket(this);
如果服務器發(fā)送連接成功,則客戶端顯示“成功與服務器建立連接”。
connect(tcpSocket, &QTcpSocket::connected,
? ? ? ? ? ? [=](){
? ? ? ? ? ? ? ? ui->textEditRead->setText("成功與服務器建立連接");
? ? ? ? ? ? }
? ? ? ? ? ? );
客戶端讀取數(shù)據(jù),并顯示在接收區(qū)
connect(tcpSocket, &QTcpSocket::readyRead,
? ? ? ? ? ? [=](){
? ? ? ? ? ? ? ? //獲取服務器發(fā)送的內(nèi)容
? ? ? ? ? ? ? ? QByteArray array = tcpSocket->readAll();
? ? ? ? ? ? ? ? ui->textEditRead->append(array);
? ? ? ? ? ? }
? ? ? ? ? ? );
按下connect按鈕,主動與服務器建立連接
void ClientWidget::on_pushButtonConnect_clicked()
{
? ? //獲取服務器ip和端口
? ? QString ip = ui->lineEditIP->text();
? ? quint16 port = ui->lineEditPort->text().toInt();
? ? //主動和服務器建立連接
? ? tcpSocket->connectToHost(QHostAddress(ip), port);
}
按下send按鈕,發(fā)送數(shù)據(jù),轉(zhuǎn)成utf-8格式
void ClientWidget::on_pushButtonSend_clicked()
{
? ? if(NULL == tcpSocket){
? ? ? ? return;
? ? }
? ? //獲取編輯區(qū)內(nèi)容
? ? QString str = ui->textEditWrite->toPlainText();
? ? //給對方發(fā)送數(shù)據(jù),使用套接字是tcpSocket
? ? tcpSocket->write(str.toUtf8().data());
}
按下close關閉連接
void ClientWidget::on_pushButtonClose_clicked()
{
? ? tcpSocket->disconnectFromHost();
? ? tcpSocket->close();
}
原文鏈接:https://blog.csdn.net/qq_31348733/article/details/95596038
相關推薦
- 2022-06-07 freertos實時操作系統(tǒng)空閑任務阻塞延時示例解析_操作系統(tǒng)
- 2022-07-02 如何對numpy?矩陣進行通道間求均值_python
- 2022-10-11 Nginx安裝&配置 Windows10
- 2022-09-15 C++中的幾個特殊符號說明_C 語言
- 2022-05-01 C語言?程序的編譯系統(tǒng)解析_C 語言
- 2022-04-03 Android實現(xiàn)recyclerview城市字母索引列表_Android
- 2022-01-09 uview 使用scroll-view以及swiper 做tabs
- 2023-04-19 Element UI Table常用使用方法(header-cell-style;表頭中的全選框取消
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支