您好,登錄后才能下訂單哦!
這篇文章主要介紹了Qt基于TCP如何實(shí)現(xiàn)客戶端與服務(wù)端連接的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Qt基于TCP如何實(shí)現(xiàn)客戶端與服務(wù)端連接文章都會(huì)有所收獲,下面我們一起來看看吧。
可以實(shí)現(xiàn)局域網(wǎng)內(nèi)簡(jiǎn)單的信息傳遞(文件傳輸,稍后更新)
界面是用ui設(shè)計(jì)師做的簡(jiǎn)單設(shè)計(jì)
(1)、ClientWidget.h 頭文件
#ifndef CLIENTWIDGET_H #define CLIENTWIDGET_H #include <QWidget> #include "ui_ClientWidget.h" #include <QTcpSocket> #include <QHostAddress> #include <QTextCodec> class ClientWidget : public QWidget, public Ui::ClientWidget { Q_OBJECT public: ClientWidget(QWidget *parent = 0); ~ClientWidget(); private slots: //連接按鈕 void onConnectButtonClicked(); // void onTextEditRead(); //發(fā)送按鈕 void onButtonSendClicked(); //獲取對(duì)方發(fā)送的內(nèi)容 void onRecesiveDataFromServer(); //斷開連接 void onDisConnect(); private: //Ui::ClientWidget ui; QTcpSocket *tcpSocket; }; #endif // CLIENTWIDGET_H
(2)、ClientWidget.cpp文件
#include "stdafx.h" #include "ClientWidget.h" #include <QPushButton> ClientWidget::ClientWidget(QWidget *parent) : QWidget(parent) { setupUi(this); setWindowTitle(QString::fromWCharArray(L"客戶端")); tcpSocket = NULL; //分配空間,指定父對(duì)象 tcpSocket = new QTcpSocket(this); ButtonDisconnect->setEnabled(false); //tcpSocket->abort(); //發(fā)送與服務(wù)器連接信號(hào) connect(connectBtn, SIGNAL(clicked()), this, SLOT(onConnectButtonClicked())); //連接成功后接收到connected信號(hào) connect(tcpSocket, SIGNAL(connected()), this, SLOT(onTextEditRead())); //給服務(wù)器發(fā)送內(nèi)容 connect(ButtonSend, SIGNAL(clicked()), this, SLOT(onButtonSendClicked())); //接收來自服務(wù)器的內(nèi)容,信號(hào)readReady connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(onRecesiveDataFromServer())); connect(ButtonDisconnect, SIGNAL(clicked()), this, SLOT(onDisConnect())); } ClientWidget::~ClientWidget() { } void ClientWidget::onConnectButtonClicked() { //獲取服務(wù)器IP和端口 QString ipStr = LineEditIPName->text(); qint16 portName = LineEditPortName->text().toInt(); QHostAddress ip = QHostAddress(ipStr); //主動(dòng)和服務(wù)器建立連接 tcpSocket->connectToHost(ip, portName); } void ClientWidget::onTextEditRead() { TextEditRead->setText(QString::fromLocal8Bit("成功和服務(wù)器建立好連接?。。?quot;)); connectBtn->setEnabled(false); ButtonDisconnect->setEnabled(true); } void ClientWidget::onButtonSendClicked() { if (tcpSocket == NULL) { return; } //獲取編輯框內(nèi)容 QString str = TextEditWrite->toPlainText(); //發(fā)送文本內(nèi)容 tcpSocket->write(str.toUtf8().data()); TextEditWrite->clear(); } void ClientWidget::onRecesiveDataFromServer() { QByteArray arrayAll = tcpSocket->readAll(); QTextCodec *tc = QTextCodec::codecForName("UTF-8"); QString str = tc->toUnicode(arrayAll); //追加到讀取編輯區(qū)中 TextEditRead->append(str); } void ClientWidget::onDisConnect() { if (tcpSocket == NULL) { return; } tcpSocket->disconnectFromHost(); tcpSocket->close(); connectBtn->setEnabled(true); ButtonDisconnect->setEnabled(false); TextEditRead->setText(QString::fromLocal8Bit("連接已斷開?。?!")); }
(1)、ServerWidget.h文件
#ifndef SERVERWIDGET_H #define SERVERWIDGET_H #include <QtGui/QWidget> #include "ui_ServerWidget.h" #include <QTcpServer> #include <QTcpSocket> #include <QString> #include <QTextCodec> class ServerWidget : public QWidget { Q_OBJECT public: ServerWidget(QWidget *parent = 0, Qt::WFlags flags = 0); ~ServerWidget(); QTcpServer *tcpServer; QTcpSocket *tcpSocket; private slots: void OnConnectTcpServer(); void OnSendButtonClicked(); void OnCloseButtonClicked(); void OnSeResiveData(); void OnDisconnected(); private: Ui::ServerWidgetClass ui; }; #endif // SERVERWIDGET_H
(2)、ServerWidget.cpp 文件
#include "stdafx.h" #include "ServerWidget.h" ServerWidget::ServerWidget(QWidget *parent, Qt::WFlags flags) : QWidget(parent, flags) { ui.setupUi(this); tcpServer = NULL; tcpSocket = NULL; setWindowTitle(QString::fromWCharArray(L"服務(wù)器(端口:8888)")); //箭筒套接字,指定父對(duì)象,讓其自動(dòng)回收空間 tcpServer = new QTcpServer(this); //監(jiān)聽并綁定端口 tcpServer->listen(QHostAddress::Any, 8888); connect(tcpServer, SIGNAL(newConnection()), this, SLOT(OnConnectTcpServer())); connect(ui.sendButton, SIGNAL(clicked()), this, SLOT(OnSendButtonClicked())); connect(ui.closeButton, SIGNAL(clicked()), this, SLOT(OnCloseButtonClicked())); connect(tcpServer, SIGNAL(disconnected()), this, SLOT(OnDisconnected())); } ServerWidget::~ServerWidget() { } #include <QDebug> void ServerWidget::OnConnectTcpServer() { //取出建立好的套接字 tcpSocket = tcpServer->nextPendingConnection(); //獲取對(duì)方的IP和端口號(hào) QString ipStr = tcpSocket->peerAddress().toString(); qint16 portName = tcpSocket->peerPort(); QString connectStr = QString::fromLocal8Bit("成功連接"); QString tempStr = QString("[%1 : %2]:" + connectStr).arg(ipStr).arg(portName); ui.textRead->setText(tempStr); connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(OnSeResiveData())); } void ServerWidget::OnSendButtonClicked() { if (tcpSocket == NULL) { return; } //獲取編輯區(qū)的內(nèi)容 QString str = ui.textWrite->toPlainText(); //給對(duì)方發(fā)送數(shù)據(jù)。使用套接字是tcpSocket tcpSocket->write(str.toUtf8().data()); ui.textWrite->clear(); } void ServerWidget::OnCloseButtonClicked() { if (tcpSocket == NULL) { return; } //主動(dòng)和客戶端斷開連接 tcpSocket->disconnectFromHost(); ui.textRead->setText(QString::fromLocal8Bit("連接已斷開?。?!")); tcpSocket = NULL; } void ServerWidget::OnSeResiveData() { //從通信套接字中取出內(nèi)容 QByteArray dataAll = tcpSocket->readAll(); QTextCodec *tc = QTextCodec::codecForName("UTF-8"); QString str = tc->toUnicode(dataAll); ui.textRead->append(str); } void ServerWidget::OnDisconnected() { ui.textRead->setText(QString::fromLocal8Bit("連接已斷開!??!")); }
關(guān)于“Qt基于TCP如何實(shí)現(xiàn)客戶端與服務(wù)端連接”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Qt基于TCP如何實(shí)現(xiàn)客戶端與服務(wù)端連接”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。