QT学习——Tcp客户端通信(本地回环)
1.Linux下Tcp通信
2.Qt下Tcp通信
????1)Tcp框架——簡圖
下面的文本框是寫入文本,上面的是顯示發送后接收到的信息。這里發送的消息并不會立刻顯示到界面上,而是要通過 readyRead() 的函數讀取,即是做接受處理。然后才會顯示發送的內容。
3.代碼如下(ui界面直接拖控件實現)
1)第一部分
Tcp-socket.pro
QT += core gui network??????//要用到網絡,所以在 .pro文件里加入network。
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Tcp-socket
TEMPLATE = app
SOURCES += main.cpp\
serverwidget.cpp \
clientwidget.cpp
HEADERS += serverwidget.h \
clientwidget.h
FORMS += serverwidget.ui \
clientwidget.ui
CONFIG += C++11???????//利用 lamda 表達式要加上這個
2)第二部分(服務器窗口代碼)
serverwidget.h??(頭文件)
#ifndef SERVERWIDGET_H
#define SERVERWIDGET_H
#include <QWidget>
#include <QTcpServer> //監聽套接字??????????只有服務器部分是需要兩個套接字的
#include <QTcpSocket> //通信套接字
namespace Ui
{
????class ServerWidget;
}
class ServerWidget : public QWidget
{
????Q_OBJECT
????public:
????explicit ServerWidget(QWidget *parent = 0);
????~ServerWidget();
????private slots:
????void on_ButtonSend_clicked();
????void on_ButtonClose_clicked();
????private:
????Ui::ServerWidget *ui;
????QTcpServer *tcpServer; //監聽套接字??????????????????????????
????QTcpSocket *tcpSocket; //通信套接字
};
#endif // SERVERWIDGET_H
serverwidget.cpp (源文件)
#include "serverwidget.h"
#include "ui_serverwidget.h"
ServerWidget::ServerWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ServerWidget)
{
????ui->setupUi(this);
????tcpServer = NULL;????????????//防止在未輸入之前點擊按鍵導致服務器沒有收到數據而出現程序異常
????tcpSocket = NULL;
????setWindowTitle("服務器:8888");
????//監聽套接字,動態分配空間。指定父對象,讓其自動回收空間
????tcpServer = new QTcpServer(this);???
????tcpServer->listen(QHostAddress::Any,????????????????????8888);
?????????????????????????????主機地址?????????綁定網卡所有IP????????端口?
????connect(tcpServer, &QTcpServer::newConnection,
????[=]()
????{??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????lamda表達式
????????//取出建立好的套接字
????????tcpSocket = tcpServer->nextPendingConnection();
????????//獲取對方的IP地址和端口
????????QString ip = tcpSocket->peerAddress().toString();
????????????????????????????????????????????獲取對方的IP地址,并將對方的地址轉換成字符串
????????qint16 port = tcpSocket->peerPort();
????????端口號是int整型????????????????????獲取對方的端口號??????????????????????
????????QString temp = QString("[%1:%2]:成功連接").arg(ip).arg(port);????//IP、端口的顯示的形式
????????ui->textEditread->setText(temp);????//顯示IP、端口
????????connect(tcpSocket, &QTcpSocket::readyRead, //這個槽函數要放在這里,讀取上面的內容。若放在外面,先執行構造函數QTcpSocket *tcpSocket 但是這個時候Tcpsocket還沒有動態分配空間,指針
????????????[=]() ?????????????????????????????????????????????????????????????//是野指針,沒有內容,程序異常結束
????????????{??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
????????????//從通信套接字中取出內容
????????????QByteArray array = tcpSocket->readAll();
????????????ui->textEditread->append(array);???//確定將內容賦值給輸入框
????????????}
????????????);
????}
????);
}
ServerWidget::~ServerWidget()
{
????delete ui;
}
void ServerWidget::on_ButtonSend_clicked()
{
????if(NULL == tcpSocket)????????//防止在未輸入之前點擊按鍵導致服務器沒有收到數據而出現程序異常
????{
????????return;
????}
????//獲取編輯區的內容
????QString str = ui->textEditwrite->toPlainText();
????//給對方發送數據,使用套接字tcpSocket
????tcpSocket->write(str.toUtf8().data());
}
void ServerWidget::on_ButtonClose_clicked()
{
????if(NULL == tcpSocket)???
????{
????????return;
????}
//主動和客戶端端口斷開
????tcpSocket->disconnectFromHost();
????tcpSocket->close();
????ui->textEditread->setText("和客戶端斷開連接");
????tcpSocket = NULL;
}
3)第三部分(客戶端)
clientwidget.h??(頭文件)
#ifndef CLIENTWIDGET_H
#define CLIENTWIDGET_H
#include <QWidget>
#include <QTcpSocket>??????//客戶端只需要一個通信套接字
namespace Ui?
{
????class ClientWidget;
}
class ClientWidget : public QWidget
{
????Q_OBJECT
????public:
????????explicit ClientWidget(QWidget *parent = 0);
????????~ClientWidget();
????private slots:
????void on_connect_clicked();
????void on_send_clicked();
????void on_close_clicked();
????private:
????Ui::ClientWidget *ui;
????QTcpSocket *tcpSocket;
};
#endif // CLIENTWIDGET_H
clientwidget.h (源文件)
#include "clientwidget.h"
#include "ui_clientwidget.h"
#include <QHostAddress>
ClientWidget::ClientWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ClientWidget)
{
????ui->setupUi(this);
????setWindowTitle("客戶端");
????tcpSocket = NULL;
????tcpSocket = new QTcpSocket(this);
????connect(tcpSocket, &QTcpSocket::connected,
????????[=]()
????????{
????????????ui->textEditRead->setText("成功和服務器連接");
????????}
????????);
????connect(tcpSocket, &QTcpSocket::readyRead,
????????[=]()
????????{
????????????//獲取對方發送的內容
????????????QByteArray array = tcpSocket->readAll();
????????????//在編輯區中顯示
????????????ui->textEditRead->append(array);
????????}
????????);
}
ClientWidget::~ClientWidget()
{
????delete ui;
}
void ClientWidget::on_connect_clicked()
{
????//獲取服務器IP和端口
????QString ip = ui->lineEditIP->text();???//獲取IP內容
????qint16 port = ui->lineEditPort->text().toInt();??????//獲取的端口號是字符串,轉換成整型
????//主動和服務器建立連接
????tcpSocket->connectToHost(QHostAddress(ip), port);
?????????????????????????主機IP????????????端口
}
void ClientWidget::on_send_clicked()
{
????//獲取編輯內容
????QString str = ui->textEditWrite->toPlainText();
????//發送數據
????tcpSocket->write(str.toUtf8().data());
}
void ClientWidget::on_close_clicked()
{
????//主動和對方斷開連接
????tcpSocket->disconnectFromHost();
????ui->textEditRead->setText("和服務器斷開連接");
}
運行結果
原文:https://blog.csdn.net/buhuiguowang/article/details/80954531?
?
總結
以上是生活随笔為你收集整理的QT学习——Tcp客户端通信(本地回环)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++框架_之Qt的信号和槽的详解
- 下一篇: s3c2440移植MQTT