基于Qt的TCP實(shí)現(xiàn)通信
本文實(shí)例為大家分享了基于Qt的TCP實(shí)現(xiàn)通信的具體代碼,供大家參考,具體內(nèi)容如下
一、tcp介紹
TCP是面向連接的可靠傳輸?shù)膮f(xié)議,協(xié)議規(guī)定通信的雙方是服務(wù)端和客戶端的兩個(gè)角色:
服務(wù)端:負(fù)責(zé)監(jiān)聽(tīng)網(wǎng)絡(luò)端口,等待客戶端的連接,用連接的socket完成信息的交互;
客戶端:負(fù)責(zé)每次連接的發(fā)起,建立連接后才可以進(jìn)行通信;
二、界面設(shè)計(jì)
服務(wù)器端
客戶端
三、具體程序設(shè)計(jì)
(1)服務(wù)器端設(shè)計(jì)
1、建立一個(gè)工程,工程名為tcpserver,類名為server。在.pro文件中加入如下代碼并保存。
QT ? ? ? += network
2、進(jìn)入server.h,添加類的前置聲明
class QTcpServer; //QTcpServer類的前置聲明 class QTcpSocket; ? //QTcpSocket類的前置聲明
添加私有對(duì)象指針
QTcpServer *tcpServer; ? ? ? ? ?//添加QTcpServer私有對(duì)象指針 QTcpSocket *socket; ? ? ? ? ? ? //添加QTcpSocket私有對(duì)象指針
添加私有槽聲明
?void tcpServer_connect(); ? ? ? //連接函數(shù) ?void read_data(); ? ? ? ? ? ? ? //讀取從client發(fā)來(lái)的信息 ?void disconnected(); ? ? ? ? ? ?//斷開(kāi)連接 ?void on_sendButton_clicked(); ? //發(fā)送數(shù)據(jù)函數(shù)
3、轉(zhuǎn)到server.cpp文件中
添加頭文件#include,然后編寫構(gòu)造函數(shù)構(gòu)造函數(shù)
Server::Server(QWidget *parent) : ? ? ? ? ? ?//構(gòu)造函數(shù) ? ? QDialog(parent), ? ? ui(new Ui::Server) { ? ? ui->setupUi(this); ? ? tcpServer = new QTcpServer(this); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //創(chuàng)建對(duì)象 ? ? if(!tcpServer->listen(QHostAddress::LocalHost,6666)) ? ? ? ? ? ? ? ? ? ? ? ? ?//調(diào)用listen監(jiān)聽(tīng)到來(lái)的連接,一旦有客戶端連接到服務(wù)器,就發(fā)射newConnection信號(hào) ? ? { ? ? ? ? qDebug()<<tcpServer->errorString(); ? ? ? ? close(); ? ? } ? ? ui->sendButton->setEnabled(false); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// 設(shè)置按鈕初始值值為false狀態(tài),即不可用 ? ? connect(tcpServer,&QTcpServer::newConnection,this,&Server::tcpServer_connect);//將newConnection信號(hào)與槽函數(shù)連接起來(lái) }
槽函數(shù)
//發(fā)送數(shù)據(jù)槽函數(shù) void Server::on_sendButton_clicked() { ? ? socket->write(ui->sendText->toPlainText().toLocal8Bit());? ? ? //通過(guò)write函數(shù)發(fā)送數(shù)據(jù) ? ? socket->flush(); ? ? ui->sendText->clear(); } //確認(rèn)連接 void Server::tcpServer_connect() { ? ? socket=tcpServer->nextPendingConnection(); ? ? QObject::connect(socket,&QTcpSocket::readyRead,this,&Server::read_data); ? ?//當(dāng)接收緩沖區(qū)有信號(hào)到來(lái)時(shí),產(chǎn)生readyRead信號(hào) ? ? QObject::connect(socket,&QTcpSocket::disconnected,this,&Server::disconnected);//當(dāng)接收到dinconnected信號(hào)時(shí),執(zhí)行disconnected函數(shù) ? ? ui->sendButton->setEnabled(true); ? ? ? ? //按鈕設(shè)置為有效 ? ? ui->label->setText(tr("連接成功!")); } //讀取客戶端發(fā)送的數(shù)據(jù) void Server::read_data() { ? ? QByteArray buffer=socket->readAll(); ? ? ? ? ?//讀取的數(shù)據(jù)放入QByteArray對(duì)象中 ? ? ui->recText->append(QString::fromLocal8Bit(buffer)); ? ? ??//將數(shù)據(jù)顯示出來(lái) } void Server::disconnected() { ? ? ui->sendButton->setEnabled(false); ? ? ? ?//斷開(kāi)連接后按鈕值設(shè)置為無(wú)效 }
(2)客戶端設(shè)計(jì)
1、建立一個(gè)工程,工程名為tcpclient,類名為client。在.pro文件中加入如下代碼并保存。
QT ? ? ? += network
2、進(jìn)入client.h,添加類的前置聲明
class QTcpSocket; ? ? //QTcpSocket類的前置聲明
定義一個(gè)套接字對(duì)象指針
QTcpSocket *tcpSocket;? ? ? ?//定義一個(gè)套接字對(duì)象指針
添加私有槽函數(shù)聲明
?void readData(); ? ? ? ? ? ? ? ? //讀取函數(shù) ?void discon(); ? ? ? ? ? ? ? ? ? //斷開(kāi)連接 ?void on_connectButton_clicked(); //連接按鈕槽函數(shù) ?void on_sendButton_clicked(); ? ?//發(fā)送按鈕槽函數(shù)
3、轉(zhuǎn)到client.cpp,
添加頭文件#include,并編寫構(gòu)造函數(shù)
Client::Client(QWidget *parent) : ? ? QDialog(parent), ? ? ui(new Ui::Client) { ? ? ui->setupUi(this); ? ? tcpSocket = new QTcpSocket(this);? ? ? ? ? //定義套接字對(duì)象 ? ? //關(guān)聯(lián)信號(hào)到自定義的槽上 ? ? QObject::connect(tcpSocket,&QTcpSocket::readyRead,this,&Client::readData); ? ? ? ? ?//有接收數(shù)據(jù)時(shí),執(zhí)行讀函數(shù) ? ? QObject::connect(tcpSocket,&QTcpSocket::disconnected,this,&Client::discon); ? ? ui->sendButton->setEnabled(false); }
槽函數(shù)
void Client::discon() { ? ? ui->sendButton->setEnabled(false); ? ? ui->connectButton->setText(tr("取消連接")); } //點(diǎn)擊連接按鈕,開(kāi)始創(chuàng)建連接 void Client::on_connectButton_clicked() { ? ? if(ui->connectButton->text()==tr("連接")) ? ? { ? ? ? ? tcpSocket->abort(); ? ? ? ? tcpSocket->connectToHost(ui->hostLineEdit->text(),ui->portLineEdit->text().toInt());//連接到指定主機(jī)的端口 ? ? ? ? if(!tcpSocket->waitForConnected(30000)) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //超時(shí)連接失敗 ? ? ? ? { ? ? ? ? ? ? qDebug()<<"Connection failed!"; ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? qDebug()<<"Connection successfully!"; ? ? ? ? ui->connectButton->setText("取消連接"); ? ? ? ? ui->sendButton->setEnabled(true); ? ? } ? ? else ? ? { ? ? ? ? tcpSocket->disconnectFromHost(); ? ? ? ? ui->connectButton->setText("連接"); ? ? ? ? ui->sendButton->setEnabled(false); ? ? } } //點(diǎn)擊發(fā)送數(shù)據(jù) void Client::on_sendButton_clicked() { ? ? QString sendData=ui->sendText->toPlainText(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //發(fā)送數(shù)據(jù)為文本框的的內(nèi)容 ? ? tcpSocket->write(sendData.toLocal8Bit()); ? ? tcpSocket->flush(); ? ? ui->sendText->clear(); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- QT網(wǎng)絡(luò)編程Tcp下C/S架構(gòu)的即時(shí)通信實(shí)例
- QT5實(shí)現(xiàn)簡(jiǎn)單的TCP通信的實(shí)現(xiàn)
- 基于QT的TCP通信服務(wù)的實(shí)現(xiàn)
- Qt中TCP協(xié)議通信詳解
- QT編寫tcp通信工具(Client篇)
- Qt網(wǎng)絡(luò)編程實(shí)現(xiàn)TCP通信
- Qt網(wǎng)絡(luò)編程之TCP通信及常見(jiàn)問(wèn)題
- QT網(wǎng)絡(luò)通信TCP客戶端實(shí)現(xiàn)詳解
- QT實(shí)現(xiàn)簡(jiǎn)單TCP通信
- QT TCP實(shí)現(xiàn)簡(jiǎn)單的通信示例
相關(guān)文章
一篇文章帶你了解C++多態(tài)的實(shí)現(xiàn)原理
這篇文章主要介紹了C++多態(tài)的實(shí)現(xiàn)機(jī)制理解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下,希望能給你帶來(lái)幫助2021-08-08C語(yǔ)言實(shí)現(xiàn)學(xué)生學(xué)籍管理系統(tǒng)課程設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)學(xué)生學(xué)籍管理系統(tǒng)課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07C++?Qt開(kāi)發(fā)之使用QUdpSocket實(shí)現(xiàn)UDP網(wǎng)絡(luò)通信
Qt 是一個(gè)跨平臺(tái)C++圖形界面開(kāi)發(fā)庫(kù),利用Qt可以快速開(kāi)發(fā)跨平臺(tái)窗體應(yīng)用程序,本文主要介紹如何運(yùn)用QUdpSocket組件實(shí)現(xiàn)基于UDP的網(wǎng)絡(luò)通信功能,需要的可以參考下2024-03-03C++基礎(chǔ)之this指針與另一種“多態(tài)”
this指針識(shí)別了同一個(gè)類的不同的對(duì)象,換句話說(shuō),this指針使得成員函數(shù)可以訪問(wèn)同一個(gè)類的不同對(duì)象。再深入一點(diǎn),this指針使得成員函數(shù)會(huì)因?yàn)閠his指針的不同而訪問(wèn)到了不同的成員變量2013-07-07C語(yǔ)言實(shí)現(xiàn)動(dòng)態(tài)版通訊錄的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C語(yǔ)言實(shí)現(xiàn)一個(gè)簡(jiǎn)單的動(dòng)態(tài)版通訊錄,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C語(yǔ)言有一定幫助,需要的可以參考一下2022-08-08