From c1fe2597f1d3b4b21e50527e5f2f2272be066a38 Mon Sep 17 00:00:00 2001
From: chenyf <1343619937@qq.com>
Date: Mon, 9 Sep 2024 07:27:43 +0800
Subject: [PATCH] first_version
---
.gitignore | 1 +
.vscode/settings.json | 54 +++++++
CMakeLists.txt | 45 ++++++
CMakeLists.txt.user | 364 ++++++++++++++++++++++++++++++++++++++++++
main.cpp | 11 ++
mainwindow.cpp | 64 ++++++++
mainwindow.h | 30 ++++
mainwindow.ui | 123 ++++++++++++++
8 files changed, 692 insertions(+)
create mode 100644 .gitignore
create mode 100644 .vscode/settings.json
create mode 100644 CMakeLists.txt
create mode 100644 CMakeLists.txt.user
create mode 100644 main.cpp
create mode 100644 mainwindow.cpp
create mode 100644 mainwindow.h
create mode 100644 mainwindow.ui
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..d163863
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+build/
\ No newline at end of file
diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 0000000..9c37a84
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,54 @@
+{
+ "cmake.generator": "MinGW Makefiles",
+ "files.associations": {
+ "ostream": "cpp",
+ "iostream": "cpp",
+ "qmainwindow": "cpp",
+ "array": "cpp",
+ "atomic": "cpp",
+ "*.tcc": "cpp",
+ "cctype": "cpp",
+ "chrono": "cpp",
+ "clocale": "cpp",
+ "cmath": "cpp",
+ "cstdarg": "cpp",
+ "cstddef": "cpp",
+ "cstdint": "cpp",
+ "cstdio": "cpp",
+ "cstdlib": "cpp",
+ "ctime": "cpp",
+ "cwchar": "cpp",
+ "cwctype": "cpp",
+ "deque": "cpp",
+ "list": "cpp",
+ "unordered_map": "cpp",
+ "vector": "cpp",
+ "exception": "cpp",
+ "algorithm": "cpp",
+ "functional": "cpp",
+ "iterator": "cpp",
+ "map": "cpp",
+ "memory": "cpp",
+ "memory_resource": "cpp",
+ "numeric": "cpp",
+ "optional": "cpp",
+ "ratio": "cpp",
+ "string": "cpp",
+ "string_view": "cpp",
+ "system_error": "cpp",
+ "tuple": "cpp",
+ "type_traits": "cpp",
+ "utility": "cpp",
+ "fstream": "cpp",
+ "initializer_list": "cpp",
+ "iosfwd": "cpp",
+ "istream": "cpp",
+ "limits": "cpp",
+ "new": "cpp",
+ "sstream": "cpp",
+ "stdexcept": "cpp",
+ "streambuf": "cpp",
+ "typeinfo": "cpp",
+ "variant": "cpp"
+ }
+}
\ No newline at end of file
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..12e5769
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,45 @@
+cmake_minimum_required(VERSION 3.5)
+
+project(socket_cmd_host LANGUAGES CXX)
+
+set(CMAKE_INCLUDE_CURRENT_DIR ON)
+
+set(CMAKE_AUTOUIC ON)
+set(CMAKE_AUTOMOC ON)
+set(CMAKE_AUTORCC ON)
+
+set(CMAKE_CXX_STANDARD 11)
+set(CMAKE_CXX_STANDARD_REQUIRED ON)
+
+# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
+# Check http://doc.qt.io/qt-5/deployment-android.html for more information.
+# They need to be set before the find_package(Qt5 ...) call.
+
+#if(ANDROID)
+# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
+# if (ANDROID_ABI STREQUAL "armeabi-v7a")
+# set(ANDROID_EXTRA_LIBS
+# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
+# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
+# endif()
+#endif()
+
+find_package(Qt5 COMPONENTS Widgets REQUIRED)
+find_package(Qt5NetWork)
+if(ANDROID)
+ add_library(socket_cmd_host SHARED
+ main.cpp
+ mainwindow.cpp
+ mainwindow.h
+ mainwindow.ui
+ )
+else()
+ add_executable(socket_cmd_host
+ main.cpp
+ mainwindow.cpp
+ mainwindow.h
+ mainwindow.ui
+ )
+endif()
+
+target_link_libraries(socket_cmd_host PRIVATE Qt5::Widgets Qt5::Network)
diff --git a/CMakeLists.txt.user b/CMakeLists.txt.user
new file mode 100644
index 0000000..0b5d3ec
--- /dev/null
+++ b/CMakeLists.txt.user
@@ -0,0 +1,364 @@
+
+
+
+
+
+ EnvironmentId
+ {2af8b88c-e5ba-4fff-b554-68f1c16ee860}
+
+
+ ProjectExplorer.Project.ActiveTarget
+ 0
+
+
+ ProjectExplorer.Project.EditorSettings
+
+ true
+ false
+ true
+
+ Cpp
+
+ CppGlobal
+
+
+
+ QmlJS
+
+ QmlJSGlobal
+
+
+ 2
+ UTF-8
+ false
+ 4
+ false
+ 80
+ true
+ true
+ 1
+ true
+ false
+ 0
+ true
+ true
+ 0
+ 8
+ true
+ 1
+ true
+ true
+ true
+ false
+
+
+
+ ProjectExplorer.Project.PluginSettings
+
+
+ -fno-delayed-template-parsing
+
+ true
+ Builtin.Questionable
+
+ true
+ Builtin.DefaultTidyAndClazy
+ 8
+
+
+
+ true
+
+
+
+
+ ProjectExplorer.Project.Target.0
+
+ mingw-gcc
+ mingw-gcc
+ {70298f91-1f6a-4f9c-afc0-abb5d1400c4a}
+ 0
+ 0
+ 0
+
+
+ CMAKE_BUILD_TYPE:STRING=Debug
+ CMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx}
+ CMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C}
+ CMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX}
+ QT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable}
+
+ 2
+ D:\WORK\QT\build-socket_cmd_host-mingw_gcc-Debug
+
+
+
+
+ all
+
+ true
+ CMakeProjectManager.MakeStep
+
+ 1
+ Build
+ Build
+ ProjectExplorer.BuildSteps.Build
+
+
+
+
+
+ clean
+
+ true
+ CMakeProjectManager.MakeStep
+
+ 1
+ Clean
+ Clean
+ ProjectExplorer.BuildSteps.Clean
+
+ 2
+ false
+
+ Debug
+ CMakeProjectManager.CMakeBuildConfiguration
+
+
+
+ CMAKE_BUILD_TYPE:STRING=Release
+ CMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx}
+ CMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C}
+ CMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX}
+ QT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable}
+
+ 2
+ D:\WORK\QT\build-socket_cmd_host-mingw_gcc-Release
+
+
+
+
+ all
+
+ true
+ CMakeProjectManager.MakeStep
+
+ 1
+ Build
+ Build
+ ProjectExplorer.BuildSteps.Build
+
+
+
+
+
+ clean
+
+ true
+ CMakeProjectManager.MakeStep
+
+ 1
+ Clean
+ Clean
+ ProjectExplorer.BuildSteps.Clean
+
+ 2
+ false
+
+ Release
+ CMakeProjectManager.CMakeBuildConfiguration
+
+
+
+ CMAKE_BUILD_TYPE:STRING=RelWithDebInfo
+ CMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx}
+ CMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C}
+ CMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX}
+ QT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable}
+
+ 2
+ D:\WORK\QT\build-socket_cmd_host-mingw_gcc-RelWithDebInfo
+
+
+
+
+ all
+
+ true
+ CMakeProjectManager.MakeStep
+
+ 1
+ Build
+ Build
+ ProjectExplorer.BuildSteps.Build
+
+
+
+
+
+ clean
+
+ true
+ CMakeProjectManager.MakeStep
+
+ 1
+ Clean
+ Clean
+ ProjectExplorer.BuildSteps.Clean
+
+ 2
+ false
+
+ Release with Debug Information
+ CMakeProjectManager.CMakeBuildConfiguration
+
+
+
+ CMAKE_BUILD_TYPE:STRING=MinSizeRel
+ CMAKE_CXX_COMPILER:FILEPATH=%{Compiler:Executable:Cxx}
+ CMAKE_C_COMPILER:FILEPATH=%{Compiler:Executable:C}
+ CMAKE_PREFIX_PATH:PATH=%{Qt:QT_INSTALL_PREFIX}
+ QT_QMAKE_EXECUTABLE:FILEPATH=%{Qt:qmakeExecutable}
+
+ 2
+ D:\WORK\QT\build-socket_cmd_host-mingw_gcc-MinSizeRel
+
+
+
+
+ all
+
+ true
+ CMakeProjectManager.MakeStep
+
+ 1
+ Build
+ Build
+ ProjectExplorer.BuildSteps.Build
+
+
+
+
+
+ clean
+
+ true
+ CMakeProjectManager.MakeStep
+
+ 1
+ Clean
+ Clean
+ ProjectExplorer.BuildSteps.Clean
+
+ 2
+ false
+
+ Minimum Size Release
+ CMakeProjectManager.CMakeBuildConfiguration
+
+ 4
+
+
+ 0
+ Deploy
+ Deploy
+ ProjectExplorer.BuildSteps.Deploy
+
+ 1
+
+ false
+ ProjectExplorer.DefaultDeployConfiguration
+
+ 1
+
+
+ dwarf
+
+ cpu-cycles
+
+
+ 250
+
+ -e
+ cpu-cycles
+ --call-graph
+ dwarf,4096
+ -F
+ 250
+
+ -F
+ true
+ 4096
+ false
+ false
+ 1000
+
+ true
+
+ false
+ false
+ false
+ false
+ true
+ 0.01
+ 10
+ true
+ kcachegrind
+ 1
+ 25
+
+ 1
+ true
+ false
+ true
+ valgrind
+
+ 0
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+ 10
+ 11
+ 12
+ 13
+ 14
+
+ 2
+
+ socket_cmd_host
+ CMakeProjectManager.CMakeRunConfiguration.socket_cmd_host
+ socket_cmd_host
+
+ false
+
+ false
+ true
+ true
+ false
+ false
+ true
+
+ C:/Users/chenyf/AppData/Local/Temp/QtCreator-gqpGWl/qtc-cmake-kVRInkbB
+
+ 1
+
+
+
+ ProjectExplorer.Project.TargetCount
+ 1
+
+
+ ProjectExplorer.Project.Updater.FileVersion
+ 22
+
+
+ Version
+ 22
+
+
diff --git a/main.cpp b/main.cpp
new file mode 100644
index 0000000..fd3e533
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,11 @@
+#include "mainwindow.h"
+
+#include
+
+int main(int argc, char *argv[])
+{
+ QApplication a(argc, argv);
+ MainWindow w;
+ w.show();
+ return a.exec();
+}
diff --git a/mainwindow.cpp b/mainwindow.cpp
new file mode 100644
index 0000000..7c477c6
--- /dev/null
+++ b/mainwindow.cpp
@@ -0,0 +1,64 @@
+#include "mainwindow.h"
+#include "./ui_mainwindow.h"
+
+MainWindow::MainWindow(QWidget *parent)
+ : QMainWindow(parent), ui(new Ui::MainWindow)
+{
+ ui->setupUi(this);
+ ui->le_port->setText("1234");
+
+ connect(ui->bt_start, &QPushButton::clicked, this, &MainWindow::start_server);
+ connect(ui->bt_stop, &QPushButton::clicked, this, &MainWindow::stop_server);
+ client = nullptr;
+}
+
+MainWindow::~MainWindow()
+{
+ delete ui;
+}
+
+void MainWindow::start_server()
+{
+ int port = ui->le_port->text().toInt();
+ if (port == 0)
+ {
+ return;
+ }
+ server.listen(QHostAddress::Any, port);
+ ui->lb_state->setText("启动成功");
+ ui->lb_listen_port->setText(QString::number(port));
+ connect(&server, &QTcpServer::newConnection, this, &MainWindow::new_connection);
+}
+
+void MainWindow::stop_server()
+{
+ server.close();
+ ui->lb_state->setText("已停止");
+ ui->lb_listen_port->setText("");
+}
+void MainWindow::new_connection()
+{
+ if (server.hasPendingConnections())
+ {
+ std::cout<< "new connection" << std::endl;
+ if (client == nullptr)
+ {
+ client = server.nextPendingConnection();
+ connect(client, &QTcpSocket::readyRead, this, &MainWindow::read_data);
+ ui->lb_client->setText("已连接");
+ connect(client, &QTcpSocket::disconnected, this, &MainWindow::disconnected);
+ }
+ }
+}
+void MainWindow::disconnected()
+{
+ ui->lb_client->setText("已断开");
+ client = nullptr;
+}
+void MainWindow::read_data()
+{
+ QTcpSocket *socket = qobject_cast(sender());
+ QByteArray data = socket->readAll();
+ QString str = data.data();
+ ui->pte_info->append(str);
+}
\ No newline at end of file
diff --git a/mainwindow.h b/mainwindow.h
new file mode 100644
index 0000000..d454045
--- /dev/null
+++ b/mainwindow.h
@@ -0,0 +1,30 @@
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#include
+#include
+#include
+#include
+QT_BEGIN_NAMESPACE
+namespace Ui { class MainWindow; }
+QT_END_NAMESPACE
+
+class MainWindow : public QMainWindow
+{
+ Q_OBJECT
+
+public:
+ MainWindow(QWidget *parent = nullptr);
+ ~MainWindow();
+public slots:
+ void start_server();
+ void stop_server();
+ void new_connection();
+ void read_data();
+ void disconnected();
+private:
+ Ui::MainWindow *ui;
+ QTcpServer server;
+ QTcpSocket *client;
+};
+#endif // MAINWINDOW_H
diff --git a/mainwindow.ui b/mainwindow.ui
new file mode 100644
index 0000000..d7f7c7c
--- /dev/null
+++ b/mainwindow.ui
@@ -0,0 +1,123 @@
+
+
+ MainWindow
+
+
+
+ 0
+ 0
+ 800
+ 596
+
+
+
+ MainWindow
+
+
+
+
+
+ 280
+ 30
+ 421
+ 401
+
+
+
+
+
+
+ 20
+ 20
+ 241
+ 161
+
+
+
+
+
+
+ -
+
+
+ Port:
+
+
+
+ -
+
+
+ -
+
+
+ 状态:
+
+
+
+ -
+
+
+
+
+
+
+ -
+
+
+ 关闭服务器
+
+
+
+ -
+
+
+ 启动服务器
+
+
+
+ -
+
+
+ 监听端口:
+
+
+
+ -
+
+
+ 未启动
+
+
+
+ -
+
+
+ 客户端:
+
+
+
+ -
+
+
+ 无连接
+
+
+
+
+
+
+
+
+
+
+
+