界面:
代码:
.pro
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp \
myserver.cpp
HEADERS += \
mainwindow.h \
myserver.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
Images.qrc
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QList>
#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
#include "myserver.h"
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::MainWindow *ui;
bool enable;
MyServer *p;
};
#endif // MAINWINDOW_H
myserver.h
#ifndef MYSERVER_H
#define MYSERVER_H
#include <QDebug>
#include <QHostAddress>
#include <QList>
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
class MyServer : public QTcpServer
{
Q_OBJECT
public:
const QList<QString> &getRecvList();
explicit MyServer(QObject *parent = nullptr);
void startServer(QHostAddress ipv4, quint16 port);
void sendMessage();
void settext(QString);
private slots:
void onNewConnection();
void onReadyRead();
void onDisconnected();
private:
QList<QTcpSocket *> clients;
QList<QString> recvList;
QString sendContext;
};
#endif // MYSERVER_H
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, enable(false)
, p(new MyServer)
{
ui->setupUi(this);
this->setWindowTitle("TCP Server");
this->setWindowIcon(QIcon(":/heads/12.jpg"));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
if (enable == true) {
ui->pushButton->setText("启动");
} else {
ui->pushButton->setText("修改");
}
ui->lineEdit_ip->setEnabled(enable);
ui->lineEdit_port->setEnabled(enable);
enable = !enable;
p->startServer(QHostAddress(ui->lineEdit_ip->text()), ui->lineEdit_port->text().toUShort());
}
void MainWindow::on_pushButton_2_clicked()
{
p->settext(ui->textEdit_send->toPlainText());
p->sendMessage();
QList<QString> buf = p->getRecvList();
for (const QString &item : buf) {
ui->textEdit_recv->append(item);
}
}
myserver.cpp
#include "myserver.h"
const QList<QString> &MyServer::getRecvList()
{
return recvList;
}
MyServer::MyServer(QObject *parent)
: QTcpServer{parent}
{
connect(this, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
}
void MyServer::startServer(QHostAddress ipv4, quint16 port)
{
if (listen(ipv4, port)) {
qDebug() << "Server start on " << ipv4.toString() << port;
} else {
qDebug() << errorString();
}
}
void MyServer::sendMessage()
{
if (clients.last()) {
clients.last()->write(sendContext.toUtf8());
}
}
void MyServer::settext(QString s1)
{
sendContext = s1;
}
void MyServer::onNewConnection()
{
QTcpSocket *clientSocket = nextPendingConnection();
if (clientSocket) {
qDebug() << "New incoming connection from" << clientSocket->peerAddress().toString();
clients.append(clientSocket);
connect(clientSocket, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
connect(clientSocket, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
}
}
void MyServer::onReadyRead()
{
QTcpSocket *clientSocket = qobject_cast<QTcpSocket *>(sender());
if (clientSocket) {
QByteArray data = clientSocket->readAll();
recvList.append(QString::fromUtf8(data));
}
}
void MyServer::onDisconnected()
{
QTcpSocket *clientSocket = qobject_cast<QTcpSocket *>(sender());
if (clientSocket) {
clients.removeOne(clientSocket);
clientSocket->deleteLater();
}
}