基于Qt的TCP實現(xiàn)通信
本文實例為大家分享了基于Qt的TCP實現(xiàn)通信的具體代碼,供大家參考,具體內(nèi)容如下
一、tcp介紹
TCP是面向連接的可靠傳輸?shù)膮f(xié)議,協(xié)議規(guī)定通信的雙方是服務端和客戶端的兩個角色:
服務端:負責監(jiān)聽網(wǎng)絡端口,等待客戶端的連接,用連接的socket完成信息的交互;
客戶端:負責每次連接的發(fā)起,建立連接后才可以進行通信;
二、界面設計
服務器端
客戶端
三、具體程序設計
(1)服務器端設計
1、建立一個工程,工程名為tcpserver,類名為server。在.pro文件中加入如下代碼并保存。
QT ? ? ? += network
2、進入server.h,添加類的前置聲明
class QTcpServer; //QTcpServer類的前置聲明 class QTcpSocket; ? //QTcpSocket類的前置聲明
添加私有對象指針
QTcpServer *tcpServer; ? ? ? ? ?//添加QTcpServer私有對象指針 QTcpSocket *socket; ? ? ? ? ? ? //添加QTcpSocket私有對象指針
添加私有槽聲明
?void tcpServer_connect(); ? ? ? //連接函數(shù) ?void read_data(); ? ? ? ? ? ? ? //讀取從client發(fā)來的信息 ?void disconnected(); ? ? ? ? ? ?//斷開連接 ?void on_sendButton_clicked(); ? //發(fā)送數(shù)據(jù)函數(shù)
3、轉到server.cpp文件中
添加頭文件#include,然后編寫構造函數(shù)構造函數(shù)
Server::Server(QWidget *parent) : ? ? ? ? ? ?//構造函數(shù) ? ? QDialog(parent), ? ? ui(new Ui::Server) { ? ? ui->setupUi(this); ? ? tcpServer = new QTcpServer(this); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //創(chuàng)建對象 ? ? if(!tcpServer->listen(QHostAddress::LocalHost,6666)) ? ? ? ? ? ? ? ? ? ? ? ? ?//調用listen監(jiān)聽到來的連接,一旦有客戶端連接到服務器,就發(fā)射newConnection信號 ? ? { ? ? ? ? qDebug()<<tcpServer->errorString(); ? ? ? ? close(); ? ? } ? ? ui->sendButton->setEnabled(false); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// 設置按鈕初始值值為false狀態(tài),即不可用 ? ? connect(tcpServer,&QTcpServer::newConnection,this,&Server::tcpServer_connect);//將newConnection信號與槽函數(shù)連接起來 }
槽函數(shù)
//發(fā)送數(shù)據(jù)槽函數(shù) void Server::on_sendButton_clicked() { ? ? socket->write(ui->sendText->toPlainText().toLocal8Bit());? ? ? //通過write函數(shù)發(fā)送數(shù)據(jù) ? ? socket->flush(); ? ? ui->sendText->clear(); } //確認連接 void Server::tcpServer_connect() { ? ? socket=tcpServer->nextPendingConnection(); ? ? QObject::connect(socket,&QTcpSocket::readyRead,this,&Server::read_data); ? ?//當接收緩沖區(qū)有信號到來時,產(chǎn)生readyRead信號 ? ? QObject::connect(socket,&QTcpSocket::disconnected,this,&Server::disconnected);//當接收到dinconnected信號時,執(zhí)行disconnected函數(shù) ? ? ui->sendButton->setEnabled(true); ? ? ? ? //按鈕設置為有效 ? ? ui->label->setText(tr("連接成功!")); } //讀取客戶端發(fā)送的數(shù)據(jù) void Server::read_data() { ? ? QByteArray buffer=socket->readAll(); ? ? ? ? ?//讀取的數(shù)據(jù)放入QByteArray對象中 ? ? ui->recText->append(QString::fromLocal8Bit(buffer)); ? ? ??//將數(shù)據(jù)顯示出來 } void Server::disconnected() { ? ? ui->sendButton->setEnabled(false); ? ? ? ?//斷開連接后按鈕值設置為無效 }
(2)客戶端設計
1、建立一個工程,工程名為tcpclient,類名為client。在.pro文件中加入如下代碼并保存。
QT ? ? ? += network
2、進入client.h,添加類的前置聲明
class QTcpSocket; ? ? //QTcpSocket類的前置聲明
定義一個套接字對象指針
QTcpSocket *tcpSocket;? ? ? ?//定義一個套接字對象指針
添加私有槽函數(shù)聲明
?void readData(); ? ? ? ? ? ? ? ? //讀取函數(shù) ?void discon(); ? ? ? ? ? ? ? ? ? //斷開連接 ?void on_connectButton_clicked(); //連接按鈕槽函數(shù) ?void on_sendButton_clicked(); ? ?//發(fā)送按鈕槽函數(shù)
3、轉到client.cpp,
添加頭文件#include,并編寫構造函數(shù)
Client::Client(QWidget *parent) : ? ? QDialog(parent), ? ? ui(new Ui::Client) { ? ? ui->setupUi(this); ? ? tcpSocket = new QTcpSocket(this);? ? ? ? ? //定義套接字對象 ? ? //關聯(lián)信號到自定義的槽上 ? ? QObject::connect(tcpSocket,&QTcpSocket::readyRead,this,&Client::readData); ? ? ? ? ?//有接收數(shù)據(jù)時,執(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("取消連接")); } //點擊連接按鈕,開始創(chuàng)建連接 void Client::on_connectButton_clicked() { ? ? if(ui->connectButton->text()==tr("連接")) ? ? { ? ? ? ? tcpSocket->abort(); ? ? ? ? tcpSocket->connectToHost(ui->hostLineEdit->text(),ui->portLineEdit->text().toInt());//連接到指定主機的端口 ? ? ? ? if(!tcpSocket->waitForConnected(30000)) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //超時連接失敗 ? ? ? ? { ? ? ? ? ? ? qDebug()<<"Connection failed!"; ? ? ? ? ? ? return; ? ? ? ? } ? ? ? ? qDebug()<<"Connection successfully!"; ? ? ? ? ui->connectButton->setText("取消連接"); ? ? ? ? ui->sendButton->setEnabled(true); ? ? } ? ? else ? ? { ? ? ? ? tcpSocket->disconnectFromHost(); ? ? ? ? ui->connectButton->setText("連接"); ? ? ? ? ui->sendButton->setEnabled(false); ? ? } } //點擊發(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(); }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C++?Qt開發(fā)之使用QUdpSocket實現(xiàn)UDP網(wǎng)絡通信
Qt 是一個跨平臺C++圖形界面開發(fā)庫,利用Qt可以快速開發(fā)跨平臺窗體應用程序,本文主要介紹如何運用QUdpSocket組件實現(xiàn)基于UDP的網(wǎng)絡通信功能,需要的可以參考下2024-03-03