日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt中Tcp通信的简单使用二

發布時間:2024/9/27 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt中Tcp通信的简单使用二 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

tcp編程中寫的一個簡單的單項傳輸數據的小例子,和上一節一樣,分為客戶端和服務端程序,下面看一下界面的效果。


創建的方法和之前一樣,上面上代碼:
客戶端
Client.h

#ifndef CLIENT_H #define CLIENT_H#include <QDialog> #include <QAbstractSocket>QT_BEGIN_NAMESPACE namespace Ui { class Client; } QT_END_NAMESPACE class QTcpSocket; class Client : public QDialog {Q_OBJECTpublic:Client(QWidget *parent = nullptr);~Client();private slots:void displayTip();void errorTip(QAbstractSocket::SocketError);void on_sendButton_clicked();void on_connectButton_clicked(bool checked);void on_textEdit_cursorPositionChanged();private:Ui::Client *ui;QTcpSocket *tcpClient;qint8 flag; }; #endif // CLIENT_H

Client.cpp

#include "client.h" #include "ui_client.h" #include <QtNetwork> #include <QMessageBox> #include <QDebug> /*問題描述 @1 再次輸入要發送的文本時,上次發送成功的提示依舊存在; @2 斷開連接后,上次發送成功的提示依舊存在;*/ Client::Client(QWidget *parent): QDialog(parent), ui(new Ui::Client),flag(0) {ui->setupUi(this);setWindowTitle(tr("客戶端"));tcpClient = new QTcpSocket(this);connect(tcpClient,&QTcpSocket::connected,this,&Client::displayTip);void (QTcpSocket:: *errorSign)(QAbstractSocket::SocketError) = &QTcpSocket::error;connect(tcpClient,errorSign,this,&Client::errorTip);ui->connectButton->setText(tr("連接"));ui->statusLabel->setText(tr("未連接"));ui->tipLabel->setText(tr(" "));ui->sendButton->setEnabled(false); }Client::~Client() {delete ui; }void Client::displayTip() {ui->statusLabel->setText(tr("連接成功"));ui->sendButton->setEnabled(true); }void Client::errorTip(QAbstractSocket::SocketError) {qDebug()<<"發生錯誤:"<<tcpClient->errorString();tcpClient->abort();ui->statusLabel->setText(tr("連接已斷開"));ui->tipLabel->setText(tr(" "));ui->connectButton->setText(tr("連接"));flag = 1;// qDebug()<<"flag = 1"; }void Client::on_sendButton_clicked() {QString str = ui->textEdit->toPlainText();QByteArray text = str.toUtf8();//返回一個字符串的utf-8形式,可以表示所有字符 // QByteArray text = str.toLatin1();//只能輸出英文和數字,即拉丁文,漢字會被?代替 // QByteArray text = str.toLocal8Bit();//只能輸出英文和數字,漢字會被亂碼qint64 bytes = tcpClient->write(text);if(bytes > 0){ // qDebug()<<"發送成功!";ui->textEdit->clear();ui->tipLabel->setText(tr("數據發送成功!"));} }void Client::on_connectButton_clicked(bool checked) {if(ui->IPLineEdit->text().isEmpty() || ui->portLineEdit->text().isEmpty()){QMessageBox::information(this,tr("警告"),tr("請查看主機名或端口是否已輸入"));return ;}if(checked || flag == 1){ui->connectButton->setText(tr("斷開連接"));ui->statusLabel->setText(tr("連接中......"));ui->tipLabel->setText(tr(" "));tcpClient->connectToHost(ui->IPLineEdit->text(),ui->portLineEdit->text().toInt());}else{ui->connectButton->setText(tr("連接"));ui->statusLabel->setText(tr("未連接"));ui->tipLabel->setText(tr(" ")); // tcpClient->disconnectFromHost();//等待所有數據傳輸完成后,關閉套接字 // tcpClient->close();//不會立即關閉套接字tcpClient->abort();//立即關閉套接字} }void Client::on_textEdit_cursorPositionChanged() {ui->tipLabel->setText(tr(" ")); }

服務端的程序:
Server.h

#ifndef SERVER_H #define SERVER_H#include <QDialog> #include <QAbstractSocket>QT_BEGIN_NAMESPACE namespace Ui { class Server; } QT_END_NAMESPACEclass QTcpServer; class QTcpSocket;class Server : public QDialog {Q_OBJECTpublic:Server(QWidget *parent = nullptr);~Server();private slots:void dataDisplayTip();void recevieData();void dealData();void errorTip(QAbstractSocket::SocketError);void on_listenButton_clicked();void on_disconnectButton_clicked();private:Ui::Server *ui;QTcpServer * myServer;QTcpSocket *socketConnected; }; #endif // SERVER_H

Server.cpp

#include "server.h" #include "ui_server.h" #include <QtNetwork> #include <QTimer> #include <QDebug>/*問題描述 @1 客戶端單一方面斷開與服務器的連接時,服務器不應該顯示為連接異常; @2 服務器在與客戶端連接的過程中,可以選擇單方面斷開與客戶端的連接; @3 客戶端發送中文時,服務端顯示亂碼;*/Server::Server(QWidget *parent): QDialog(parent), ui(new Ui::Server) {ui->setupUi(this);myServer = new QTcpServer(this);connect(myServer,&QTcpServer::newConnection,this,&Server::recevieData); // connect(myServer,&QTcpServer::connected,this,&Server::tipInfo);ui->statusLabel->setText(tr("未建立連接"));ui->RecevieDataLabel->setText(tr(" "));}Server::~Server() {delete ui; }void Server::dataDisplayTip() {ui->RecevieDataLabel->setText(tr(" ")); }void Server::recevieData() {ui->statusLabel->setText(tr("連接成功"));socketConnected = myServer->nextPendingConnection();connect(socketConnected,&QTcpSocket::readyRead,this,&Server::dealData);void (QTcpSocket:: *errorSign)(QAbstractSocket::SocketError) = &QTcpSocket::error;connect(socketConnected,errorSign,this,&Server::errorTip);myServer->close(); }void Server::dealData() {if(socketConnected->bytesAvailable()>0){QString str = socketConnected->readAll();//QByteArray dataui->textEdit->append(str);ui->RecevieDataLabel->setText(tr("消息接收完成!"));QTimer *timer = new QTimer(this);connect(timer,SIGNAL(timeout()),this,SLOT(dataDisplayTip()));timer->start(2000);} }void Server::errorTip(QAbstractSocket::SocketError) {qDebug()<<socketConnected->errorString();ui->listenButton->setEnabled(true);socketConnected->close();myServer->close();ui->statusLabel->setText(tr("連接中斷"));ui->RecevieDataLabel->setText(tr(" ")); }void Server::on_listenButton_clicked() {if(!myServer->listen(QHostAddress::LocalHost,6787)){qDebug()<<"監聽失敗!";ui->statusLabel->setText(tr("監聽失敗!"));myServer->close();}ui->statusLabel->setText(tr("監聽成功"));ui->RecevieDataLabel->setText(tr(" "));ui->listenButton->setEnabled(false); }void Server::on_disconnectButton_clicked() {ui->statusLabel->setText(tr("連接中斷"));ui->RecevieDataLabel->setText(tr(" "));ui->listenButton->setEnabled(true);socketConnected->disconnectFromHost();myServer->close(); }

先運行服務端程序,點擊監聽按鈕,再運行客戶端程序,輸入IP和端口,同樣程序之間實現的是本地地址通信,端口6787,主機名或ip都可以,點擊連接,若主機名和IP沒有填寫,直接點擊連接,會彈出提示窗口,提示查看IP和端口的填寫情況,對話框下面會顯示連接狀態,連接成功后,輸入要發送的內容,點擊發送按鈕可以發送信息,會有相應的發送和接收數據成功與否的顯示。
效果圖如下:

該注意的點:
在數據傳輸的過程中,編碼格式的一致,否則會導致亂碼,代碼中含有我對與一些函數的理解,加以記錄,以備后用。

總結

以上是生活随笔為你收集整理的Qt中Tcp通信的简单使用二的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。