效果图:
理清TCP通信逻辑------最重要
代码部分:
tcpserve.h
#ifndef TCPSERVE_H
#define TCPSERVE_H
#include<QTcpServer> // 监听套接字
#include<QTcpSocket> //通信套接字
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class TcpServe; }
QT_END_NAMESPACE
class TcpServe : public QWidget
{
Q_OBJECT
public:
TcpServe(QWidget *parent = nullptr);
~TcpServe();
private slots:
void on_buttonSend_clicked();
void on_buttonClose_clicked();
private:
Ui::TcpServe *ui;
QTcpServer *tcpServer; // 监听套接字
QTcpSocket * tcpSocket; //通信套接字
};
#endif // TCPSERVE_H
tcpserve.cpp
#include "tcpserve.h"
#include "ui_tcpserve.h"
TcpServe::TcpServe(QWidget *parent)
: QWidget(parent)
, ui(new Ui::TcpServe)
{
ui->setupUi(this);
tcpServer =NULL;
tcpSocket = NULL;
//监听套接字,指定父对象,让其自动回收空间,this指定父对象
tcpServer = new QTcpServer(this);
//监听IP地址和端口号
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);
connect(tcpSocket,&QTcpSocket::readyRead,
[=]()
{
//从通信套接字中取出内容
QByteArray array = tcpSocket->readAll();
ui->textEditRead->append(array);
});
});
}
TcpServe::~TcpServe()
{
delete ui;
}
//发送
void TcpServe::on_buttonSend_clicked()
{
if(tcpSocket == NULL)
{
return;
}
//获取编辑器内容
QString str = ui->textEditWrite->toPlainText();
//给对方发送数据,使用套接字是tcpSocket
tcpSocket->write(str.toUtf8().data());
}
//关闭按钮
void TcpServe::on_buttonClose_clicked()
{
if(tcpSocket == NULL)
{
return;
}
//主动和客户端端口连接
tcpSocket->disconnectFromHost();
tcpSocket->close();
tcpSocket = NULL; //断开后重新赋值为空
}
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_buttonConnect_clicked();
void on_buttonSend_clicked();
void on_buttonClose_clicked();
private:
Ui::ClientWidget *ui;
QTcpSocket * tcpSocket; //通信套接字
};
#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;
//分配空间,指定父对象
tcpSocket = new QTcpSocket(this);
setWindowTitle("客户端");
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_buttonConnect_clicked()
{
//获取服务器IP和端口
QString ip = ui->lineEditIP->text();
qint16 port =ui->lineEditPort->text().toInt();
//主动和服务器建立连接
tcpSocket->connectToHost(QHostAddress(ip),port);
}
//发送
void ClientWidget::on_buttonSend_clicked()
{
//获取编辑框内容
QString str = ui->textEditWrite->toPlainText();
//发送数据
tcpSocket->write(str.toUtf8().data());
}
//关闭
void ClientWidget::on_buttonClose_clicked()
{
//主动断开连接
tcpSocket->disconnectFromHost();
tcpSocket->close();
}
main.cpp
#include "tcpserve.h"
#include <QApplication>
#include "clientwidget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
TcpServe w;
w.show();
ClientWidget w2;
w2.show();
return a.exec();
}
tcpserv.ui
clientwidget.ui
为了防止小白(当然我也是小白)在实现的过程中出现这样那样的错误,我把所有的代码和需要注意的地方都贴了出来,如果再实现不了,就该找自身的问题了。
本来想把源码打包上传赚积分呢,想想还是为咱们小白省点吧。
资源来自黑马的QT入门视频,自己跟着敲的