【重点强调】
一定先在.pro文件中添加:
QT += network //这是你引用头文件 TcpServer 和 TcpSocket 的前提
一、客户端控件名称展示
(布局可依照自己自己喜好设置,控件名字要是看不明白,私信我,给你开小灶。。。)
二、客户端头文件代码展示 clientwidget.h
#ifndef CLIENTWIDGET_H
#define CLIENTWIDGET_H
#include <QWidget>
#include <QDebug>
#include <QTcpSocket>
QT_BEGIN_NAMESPACE
namespace Ui { class clientwidget; }
QT_END_NAMESPACE
class clientwidget : public QWidget
{
Q_OBJECT
public:
clientwidget(QWidget *parent = nullptr);
~clientwidget();
private:
Ui::clientwidget *ui;
QTcpSocket *tcpSocket; //用于通信的套接字
private slots:
void buttonConnect(); //连接按钮的槽函数
void buttonSend(); //发送按钮的槽函数
void buttonClose(); //关闭按钮的槽函数
};
#endif // CLIENTWIDGET_H
三、客户端.cpp文件展示 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;
setWindowTitle("客户端");
//建立用于通信的套接字
tcpSocket = new QTcpSocket(this);
//建立三个按钮的槽函数
connect(ui->pushButton_connect,SIGNAL(clicked()),this,SLOT(buttonConnect()));
connect(ui->pushButton_send,SIGNAL(clicked()),this,SLOT(buttonSend()));
connect(ui->pushButton_close,SIGNAL(clicked()),this,SLOT(buttonClose()));
//设置套接字连接成功的提示
connect(tcpSocket,&QTcpSocket::connected,
[=]()
{
ui->textEdit_read->append("连接成功!");
}
);
}
//客户端的析构函数
clientwidget::~clientwidget()
{
delete ui;
}
//连接按钮的槽函数实现
void clientwidget::buttonConnect()
{
QString ip = ui->lineEdit_ip->text();
qint16 port = ui->lineEdit_port->text().toInt();
tcpSocket->connectToHost(QHostAddress(ip),port);
}
//发送按钮的槽函数实现
void clientwidget::buttonSend()
{
qint16 add1 = ui->lineEdit->text().toInt();
qint16 add2 = ui->lineEdit_2->text().toInt();
qint16 add3= add1 + add2;
QString str = QString::number(add3);
qDebug()<<add3<<str;
tcpSocket->write(str.toUtf8().data());
}
//关闭按钮的槽函数实现
void clientwidget::buttonClose()
{
ui->textEdit_read->append("客户端退出");
if(tcpSocket==NULL)
return ;
tcpSocket->disconnectFromHost();
tcpSocket->close();
tcpSocket=NULL;
}
四、服务器端控件名称展示
五、服务器端头文件展示 serwidget.h
#ifndef SERWIDGET_H
#define SERWIDGET_H
#include <QWidget>
#include <QTcpSocket>
#include <QTcpServer>
#include <QDebug>
namespace Ui {
class serwidget;
}
class serwidget : public QWidget
{
Q_OBJECT
public:
explicit serwidget(QWidget *parent = nullptr);
~serwidget();
private:
Ui::serwidget *ui;
QTcpServer *tcpServer; //用于连接的套接字
QTcpSocket *tcpSocket; //用于通信的套接字
private slots:
void buttonClose(); //关闭按钮的槽函数
};
#endif // SERWIDGET_H
六、服务器端.cpp文件展示 serwidget.cpp
#include "serwidget.h"
#include "ui_serwidget.h"
serwidget::serwidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::serwidget)
{
ui->setupUi(this);
tcpServer=NULL;
tcpSocket=NULL;
//建立关闭按钮的槽函数
connect(ui->pushButton_close,SIGNAL(clicked()),this,SLOT(buttonClose));
//设置窗口名称
setWindowTitle("服务器");
//1.建立用于连接的套接字
tcpServer = new QTcpServer(this);
//2.监听
tcpServer->listen(QHostAddress::Any,8888);
//3.设置套接字通信
connect(tcpServer,&QTcpServer::newConnection,
[=]()
{
//取出建立好的链接
tcpSocket=tcpServer->nextPendingConnection();
//获取对方的ip和port
QString ip = tcpSocket->peerAddress().toString();
qint16 port = tcpSocket->peerPort();
QString temp = QString("[%1:%2]").arg(ip).arg(port);
ui->lineEdit_read->setText(temp);
//获取接收到的内容
connect(tcpSocket,&QTcpSocket::readyRead,
[=]()
{
QByteArray barry = tcpSocket->readAll();
ui->lineEdit_sum->setText(barry);
}
);
}
);
}
//服务器的析构函数
serwidget::~serwidget()
{
delete ui;
}
//关闭按钮的槽函数实现
void serwidget::buttonClose()
{
//判断通信套接字是否存在
if(tcpSocket==NULL)
return ;
//如果套接字存在
tcpSocket->disconnectFromHost();
tcpSocket->close();
tcpSocket=NULL;
}
七、main.cpp代码展示
#include "clientwidget.h"
#include "serwidget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
clientwidget w;
serwidget w2;
w.show();
w2.show();
return a.exec();
}
(有钱的打点赏儿,没钱的捧个人场)