Qt下Tcp传输原理图:
运行结果图:![](https://i-blog.csdnimg.cn/blog_migrate/ba39cecb53b0150881302795827b4ba9.png)
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 = nullptr);
~ClientWidget();
private slots:
void on_pushButtonconnect_clicked();
void on_pushButtonsend_clicked();
void on_pushButtonclose_clicked();
private:
Ui::ClientWidget *ui;
QTcpSocket* socket;
int a;
};
#endif // CLIENTWIDGET_H
ClientWidget.cpp文件
#include "clientwidget.h"
#include "ui_clientwidget.h"
#include <QHostAddress>
ClientWidget::ClientWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::ClientWidget)
{
ui->setupUi(this);
// tcpsocket=NULL;
//分配空间,指定父对象
socket=new QTcpSocket(this);
connect(socket,&QTcpSocket::connected,
[=]()
{
ui->textEditread->setText("成功和服务器建立连接");
});
connect(socket,&QTcpSocket::readyRead,
[=]()
{
QByteArray array= socket->readAll();
ui->textEditread->append(array);
});
//断开连接
connect(socket,&QTcpSocket::disconnected,
[=]()
{
ui->textEditread->setText("服务器已断开");
});
}
ClientWidget::~ClientWidget()
{
delete ui;
}
void ClientWidget::on_pushButtonconnect_clicked()
{
//获取服务器IP和端口
QString ip=ui->lineEditIp->text();
qint16 port= ui->lineEditport->text().toInt();
//主动和服务器建立链接
socket->connectToHost(QHostAddress(ip),port);
}
void ClientWidget::on_pushButtonsend_clicked()
{
//获取编辑区内容
QString str= ui->textEditwrite->toPlainText();
socket->write(str.toUtf8().data());
}
void ClientWidget::on_pushButtonclose_clicked()
{
//
//主动和对方断开连接
socket->disconnectFromHost();
socket->close();
}
ServerWidget.h文件
#ifndef SERVERWIDGET_H
#define SERVERWIDGET_H
#include <QWidget>
#include<QTcpServer>//服务器监听套接字
#include<QTcpSocket>//套接字
QT_BEGIN_NAMESPACE
namespace Ui { class ServerWidget; }
QT_END_NAMESPACE
class ServerWidget : public QWidget
{
Q_OBJECT
public:
ServerWidget(QWidget *parent = nullptr);
~ServerWidget();
private slots:
void on_ButtonSend_clicked();
void on_ButtonClose_clicked();
private:
Ui::ServerWidget *ui;
QTcpServer* tcpserver;//监听套接字
QTcpSocket* tcpsocket;//通信套接字
int a;
};
#endif // SERVERWIDGET_H
ServerWidget.cpp文件
#include "serverwidget.h"
#include "ui_serverwidget.h"
#include<QDebug>
ServerWidget::ServerWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::ServerWidget)
{
ui->setupUi(this);
tcpserver=NULL;
tcpsocket=NULL;
//监听套接字,指定父对象,让其自动回收空间
tcpserver=new QTcpServer(this);
tcpserver->listen(QHostAddress::Any,8888);
setWindowTitle("服务器:8888");
//监听到信号,取出套接字
connect(tcpserver,&QTcpServer::newConnection,
[=]()
{
//取出建立好连接的套接字
tcpsocket=tcpserver->nextPendingConnection();
//获取对方的ip和端口
QString ip=tcpsocket->peerAddress().toString();
qint16 port=tcpsocket->peerPort();
QString temp=QString("[%1:%2]:成功连接").arg(ip).arg(port);
ui->textEditRead->setText(temp);
//通信write后,触发套接字readRead信号
connect(tcpsocket,&QTcpSocket::readyRead,
[=]()
{
//从通信套接字中取出内容
QByteArray array=tcpsocket->readAll();
ui->textEditRead->append(array);
});
//断开连接
connect(tcpsocket,&QTcpSocket::disconnected,
[=]()
{
ui->textEditRead->setText("服务器已断开");
});
});
}
ServerWidget::~ServerWidget()
{
delete ui;
//服务器发送
}
void ServerWidget::on_ButtonSend_clicked()
{
if(tcpsocket==NULL)
{
return;
}
//获取编辑区内容
QString str= ui->textEditWrite->toPlainText();
//给对方发送数据,使用套接字是tcpsocket
tcpsocket->write(str.toUtf8().data());
}
//关闭
void ServerWidget::on_ButtonClose_clicked()
{
if(tcpsocket==NULL)
{
return;
}
//主动和客户端断开连接
tcpsocket->disconnectFromHost();
//关闭IO
tcpsocket->close();
tcpsocket=NULL;
}
main.cpp
#include "serverwidget.h"
#include <QApplication>
#include<clientwidget.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ServerWidget w;
w.show();
ClientWidget w2;
w2.show();
return a.exec();
}