first commit

This commit is contained in:
Msy-msu 2023-01-05 19:50:19 +08:00
commit 2992ccd00d
11 changed files with 1093 additions and 0 deletions

27
TcpServer.pro Normal file
View File

@ -0,0 +1,27 @@
QT += core gui network sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# 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 += \
database.cpp \
datamanager.cpp \
main.cpp \
protocol.cpp \
tcpserver.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
database.h \
datamanager.h \
protocol.h \
tcpserver.h

430
TcpServer.pro.user Normal file
View File

@ -0,0 +1,430 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 6.0.2, 2023-01-05T18:40:40. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
<value type="QByteArray">{d3fed20c-1c94-44fc-8f6a-4dbf39b54775}</value>
</data>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>
<value type="qlonglong">0</value>
</data>
<data>
<variable>ProjectExplorer.Project.EditorSettings</variable>
<valuemap type="QVariantMap">
<value type="bool" key="EditorConfiguration.AutoIndent">true</value>
<value type="bool" key="EditorConfiguration.AutoSpacesForTabs">false</value>
<value type="bool" key="EditorConfiguration.CamelCaseNavigation">true</value>
<valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.0">
<value type="QString" key="language">Cpp</value>
<valuemap type="QVariantMap" key="value">
<value type="QByteArray" key="CurrentPreferences">CppGlobal</value>
</valuemap>
</valuemap>
<valuemap type="QVariantMap" key="EditorConfiguration.CodeStyle.1">
<value type="QString" key="language">QmlJS</value>
<valuemap type="QVariantMap" key="value">
<value type="QByteArray" key="CurrentPreferences">QmlJSGlobal</value>
</valuemap>
</valuemap>
<value type="qlonglong" key="EditorConfiguration.CodeStyle.Count">2</value>
<value type="QByteArray" key="EditorConfiguration.Codec">UTF-8</value>
<value type="bool" key="EditorConfiguration.ConstrainTooltips">false</value>
<value type="int" key="EditorConfiguration.IndentSize">4</value>
<value type="bool" key="EditorConfiguration.KeyboardTooltips">false</value>
<value type="int" key="EditorConfiguration.MarginColumn">80</value>
<value type="bool" key="EditorConfiguration.MouseHiding">true</value>
<value type="bool" key="EditorConfiguration.MouseNavigation">true</value>
<value type="int" key="EditorConfiguration.PaddingMode">1</value>
<value type="bool" key="EditorConfiguration.PreferSingleLineComments">false</value>
<value type="bool" key="EditorConfiguration.ScrollWheelZooming">true</value>
<value type="bool" key="EditorConfiguration.ShowMargin">false</value>
<value type="int" key="EditorConfiguration.SmartBackspaceBehavior">0</value>
<value type="bool" key="EditorConfiguration.SmartSelectionChanging">true</value>
<value type="bool" key="EditorConfiguration.SpacesForTabs">true</value>
<value type="int" key="EditorConfiguration.TabKeyBehavior">0</value>
<value type="int" key="EditorConfiguration.TabSize">8</value>
<value type="bool" key="EditorConfiguration.UseGlobal">true</value>
<value type="bool" key="EditorConfiguration.UseIndenter">false</value>
<value type="int" key="EditorConfiguration.Utf8BomBehavior">1</value>
<value type="bool" key="EditorConfiguration.addFinalNewLine">true</value>
<value type="bool" key="EditorConfiguration.cleanIndentation">true</value>
<value type="bool" key="EditorConfiguration.cleanWhitespace">true</value>
<value type="QString" key="EditorConfiguration.ignoreFileTypes">*.md, *.MD, Makefile</value>
<value type="bool" key="EditorConfiguration.inEntireDocument">false</value>
<value type="bool" key="EditorConfiguration.skipTrailingWhitespace">true</value>
</valuemap>
</data>
<data>
<variable>ProjectExplorer.Project.PluginSettings</variable>
<valuemap type="QVariantMap">
<valuemap type="QVariantMap" key="AutoTest.ActiveFrameworks">
<value type="bool" key="AutoTest.Framework.Boost">true</value>
<value type="bool" key="AutoTest.Framework.CTest">false</value>
<value type="bool" key="AutoTest.Framework.Catch">true</value>
<value type="bool" key="AutoTest.Framework.GTest">true</value>
<value type="bool" key="AutoTest.Framework.QtQuickTest">true</value>
<value type="bool" key="AutoTest.Framework.QtTest">true</value>
</valuemap>
<valuemap type="QVariantMap" key="AutoTest.CheckStates"/>
<value type="int" key="AutoTest.RunAfterBuild">0</value>
<value type="bool" key="AutoTest.UseGlobal">true</value>
<valuelist type="QVariantList" key="ClangCodeModel.CustomCommandLineKey">
<value type="QString">-fno-delayed-template-parsing</value>
</valuelist>
<value type="bool" key="ClangCodeModel.UseGlobalConfig">true</value>
<value type="QString" key="ClangCodeModel.WarningConfigId">Builtin.BuildSystem</value>
<valuemap type="QVariantMap" key="ClangTools">
<value type="bool" key="ClangTools.AnalyzeOpenFiles">true</value>
<value type="bool" key="ClangTools.BuildBeforeAnalysis">true</value>
<value type="QString" key="ClangTools.DiagnosticConfig">Builtin.DefaultTidyAndClazy</value>
<value type="int" key="ClangTools.ParallelJobs">4</value>
<valuelist type="QVariantList" key="ClangTools.SelectedDirs"/>
<valuelist type="QVariantList" key="ClangTools.SelectedFiles"/>
<valuelist type="QVariantList" key="ClangTools.SuppressedDiagnostics"/>
<value type="bool" key="ClangTools.UseGlobalSettings">true</value>
</valuemap>
</valuemap>
</data>
<data>
<variable>ProjectExplorer.Project.Target.0</variable>
<valuemap type="QVariantMap">
<value type="QString" key="DeviceType">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 6.2.3 MSVC2019 64bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 6.2.3 MSVC2019 64bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt6.623.win64_msvc2019_64_kit</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
<value type="int" key="EnableQmlDebugging">0</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:\Users\25761\Desktop\build-TcpServer-Desktop_Qt_6_2_3_MSVC2019_64bit-Debug</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory.shadowDir">C:/Users/25761/Desktop/build-TcpServer-Desktop_Qt_6_2_3_MSVC2019_64bit-Debug</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">QtProjectManager.QMakeBuildStep</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.QMakeForced">false</value>
<valuelist type="QVariantList" key="QtProjectManager.QMakeBuildStep.SelectedAbis"/>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.1">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeArguments">clean</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">2</value>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.CustomParsers"/>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ParseStandardOutput">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Debug</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4BuildConfiguration</value>
<value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">2</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.1">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:\Users\25761\Desktop\build-TcpServer-Desktop_Qt_6_2_3_MSVC2019_64bit-Release</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory.shadowDir">C:/Users/25761/Desktop/build-TcpServer-Desktop_Qt_6_2_3_MSVC2019_64bit-Release</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">QtProjectManager.QMakeBuildStep</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.QMakeForced">false</value>
<valuelist type="QVariantList" key="QtProjectManager.QMakeBuildStep.SelectedAbis"/>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.1">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeArguments">clean</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">2</value>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.CustomParsers"/>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ParseStandardOutput">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Release</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4BuildConfiguration</value>
<value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">0</value>
<value type="int" key="QtQuickCompiler">0</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.2">
<value type="int" key="EnableQmlDebugging">0</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:\Users\25761\Desktop\build-TcpServer-Desktop_Qt_6_2_3_MSVC2019_64bit-Profile</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory.shadowDir">C:/Users/25761/Desktop/build-TcpServer-Desktop_Qt_6_2_3_MSVC2019_64bit-Profile</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">QtProjectManager.QMakeBuildStep</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.QMakeForced">false</value>
<valuelist type="QVariantList" key="QtProjectManager.QMakeBuildStep.SelectedAbis"/>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.1">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeArguments">clean</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">2</value>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.CustomParsers"/>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ParseStandardOutput">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Profile</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4BuildConfiguration</value>
<value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">0</value>
<value type="int" key="QtQuickCompiler">0</value>
<value type="int" key="SeparateDebugInfo">0</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.BuildConfigurationCount">3</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.DeployConfiguration.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">0</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Deploy</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Deploy</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Deploy</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">1</value>
<valuemap type="QVariantMap" key="ProjectExplorer.DeployConfiguration.CustomData"/>
<value type="bool" key="ProjectExplorer.DeployConfiguration.CustomDataEnabled">false</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.DefaultDeployConfiguration</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.DeployConfigurationCount">1</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.RunConfiguration.0">
<value type="bool" key="Analyzer.Perf.Settings.UseGlobalSettings">true</value>
<value type="bool" key="Analyzer.QmlProfiler.Settings.UseGlobalSettings">true</value>
<value type="bool" key="Analyzer.Valgrind.Settings.UseGlobalSettings">true</value>
<valuelist type="QVariantList" key="CustomOutputParsers"/>
<value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:C:/Users/25761/Desktop/TcpServer/TcpServer.pro</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey">C:/Users/25761/Desktop/TcpServer/TcpServer.pro</value>
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseLibrarySearchPath">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">C:/Users/25761/Desktop/build-TcpServer-Desktop_Qt_6_2_3_MSVC2019_64bit-Debug</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>
</data>
<data>
<variable>ProjectExplorer.Project.Target.1</variable>
<valuemap type="QVariantMap">
<value type="QString" key="DeviceType">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 6.2.3 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 6.2.3 MinGW 64-bit</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt6.623.win64_mingw_kit</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="qlonglong" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
<value type="int" key="EnableQmlDebugging">0</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:\Users\25761\Desktop\build-TcpServer-Desktop_Qt_6_2_3_MinGW_64_bit-Debug</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory.shadowDir">C:/Users/25761/Desktop/build-TcpServer-Desktop_Qt_6_2_3_MinGW_64_bit-Debug</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">QtProjectManager.QMakeBuildStep</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.QMakeForced">false</value>
<valuelist type="QVariantList" key="QtProjectManager.QMakeBuildStep.SelectedAbis"/>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.1">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeArguments">clean</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">2</value>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.CustomParsers"/>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ParseStandardOutput">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Debug</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4BuildConfiguration</value>
<value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">2</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.1">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:\Users\25761\Desktop\build-TcpServer-Desktop_Qt_6_2_3_MinGW_64_bit-Release</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory.shadowDir">C:/Users/25761/Desktop/build-TcpServer-Desktop_Qt_6_2_3_MinGW_64_bit-Release</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">QtProjectManager.QMakeBuildStep</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.QMakeForced">false</value>
<valuelist type="QVariantList" key="QtProjectManager.QMakeBuildStep.SelectedAbis"/>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.1">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeArguments">clean</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">2</value>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.CustomParsers"/>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ParseStandardOutput">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Release</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4BuildConfiguration</value>
<value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">0</value>
<value type="int" key="QtQuickCompiler">0</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.2">
<value type="int" key="EnableQmlDebugging">0</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">C:\Users\25761\Desktop\build-TcpServer-Desktop_Qt_6_2_3_MinGW_64_bit-Profile</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory.shadowDir">C:/Users/25761/Desktop/build-TcpServer-Desktop_Qt_6_2_3_MinGW_64_bit-Profile</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">QtProjectManager.QMakeBuildStep</value>
<value type="bool" key="QtProjectManager.QMakeBuildStep.QMakeForced">false</value>
<valuelist type="QVariantList" key="QtProjectManager.QMakeBuildStep.SelectedAbis"/>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.1">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Build</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.1">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.MakeStep</value>
<value type="QString" key="Qt4ProjectManager.MakeStep.MakeArguments">clean</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Clean</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">2</value>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ClearSystemEnvironment">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.CustomParsers"/>
<value type="bool" key="ProjectExplorer.BuildConfiguration.ParseStandardOutput">false</value>
<valuelist type="QVariantList" key="ProjectExplorer.BuildConfiguration.UserEnvironmentChanges"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Profile</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4BuildConfiguration</value>
<value type="int" key="Qt4ProjectManager.Qt4BuildConfiguration.BuildConfiguration">0</value>
<value type="int" key="QtQuickCompiler">0</value>
<value type="int" key="SeparateDebugInfo">0</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.BuildConfigurationCount">3</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.DeployConfiguration.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">0</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Deploy</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Deploy</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Deploy</value>
</valuemap>
<value type="int" key="ProjectExplorer.BuildConfiguration.BuildStepListCount">1</value>
<valuemap type="QVariantMap" key="ProjectExplorer.DeployConfiguration.CustomData"/>
<value type="bool" key="ProjectExplorer.DeployConfiguration.CustomDataEnabled">false</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.DefaultDeployConfiguration</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.DeployConfigurationCount">1</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.RunConfiguration.0">
<value type="bool" key="Analyzer.Perf.Settings.UseGlobalSettings">true</value>
<value type="bool" key="Analyzer.QmlProfiler.Settings.UseGlobalSettings">true</value>
<value type="bool" key="Analyzer.Valgrind.Settings.UseGlobalSettings">true</value>
<valuelist type="QVariantList" key="CustomOutputParsers"/>
<value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value>
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>
</data>
<data>
<variable>ProjectExplorer.Project.TargetCount</variable>
<value type="qlonglong">2</value>
</data>
<data>
<variable>ProjectExplorer.Project.Updater.FileVersion</variable>
<value type="int">22</value>
</data>
<data>
<variable>Version</variable>
<value type="int">22</value>
</data>
</qtcreator>

169
database.cpp Normal file
View File

@ -0,0 +1,169 @@
#include <QSql>
#include <QSqlDatabase>
#include <QDebug>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include "database.h"
DataBase *DataBase::getInstance()
{
static DataBase sqldb;
return &sqldb;
}
DataBase::DataBase()
{
//先判断连接是否存在
if(QSqlDatabase::contains("testConnection"))
{
db_ = QSqlDatabase::addDatabase("testConnection");
}
else
{
//1.加载数据库驱动
db_ = QSqlDatabase::addDatabase("QSQLITE", "testConnection");
qDebug () << db_.connectionName();//打印连接名称
//2.设置数据库名
db_.setDatabaseName("usr.db");
qDebug () << db_.databaseName();
}
//3.打开数据库
if(db_.open())
{
qDebug() << "open success";
}
else
{
qDebug() << "open:" << db_.lastError().text();
return;
}
QString sql = "create table if not exists table_usr(name varchar(30) primary key, pwd varchar(20));";
QSqlQuery query(db_);
if (query.exec(sql))
{
qDebug() << "create success.";
}
db_.close();
}
//插入一个用户信息
bool DataBase::insertUsr(QString name, QString pwd)
{
QString sql = QString("insert into table_usr values('%1', '%2')")
.arg(name)
.arg(pwd);
// 1.打开数据
if (!db_.open())
{
qDebug() << "open:" << db_.lastError().text();
return false;
}
// 2.执行sql
QSqlQuery query(db_);
if (!query.exec(sql))
{
qDebug() << "insert:" << query.lastError().text();
return false;
}
//关闭数据库
db_.close();
return true;
}
//根据name查找用户
bool DataBase::findUsr(QString name)
{
QString sql = QString(" from table_usr where name = :name;");
//打开数据库
if(!db_.open())
{
qDebug() << "open fail" << db_.lastError().text();
exit(-1);
}
//执行插入操作
QSqlQuery query(db_);
query.prepare(sql);
query.bindValue(":name",QVariant(name));
query.exec();
//查询成功
if(query.next())
{
if(name == query.value(0).toString()) //用户存在
{
return true;
}
}
db_.close();
return false;
}
bool DataBase::Delete(QString name)
{
QString sql = QString("select name from table_usr where name = :name;");
//打开数据库
if(!db_.open())
{
qDebug() << "open fail" << db_.lastError().text();
exit(-1);
}
//执行删除操作
QSqlQuery query(db_);
if (!query.exec(sql))
{
qDebug() << "delete" << query.lastError().text();
return false;
}
//关闭数据库
db_.close();
return true;
}
//匹配name和pwd
bool DataBase::match(QString name, QString pwd)
{
QString sql = QString("select name, pwd from table_usr where name = :name;");
//打开数据库
if(!db_.open())
{
qDebug() << "open fail" << db_.lastError().text();
exit(-1);
}
//执行插入操作
QSqlQuery query(db_);
query.prepare(sql);
query.bindValue(":name",QVariant(name));
query.exec();
//匹配密码
if(query.next())
{
if(!(pwd == query.value(1).toString()))
{
return false;
}
}
//关闭数据库
db_.close();
return true;
}

32
database.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef DATABASE_H
#define DATABASE_H
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QVector>
#include <QString>
class DataBase
{
public:
static DataBase *getInstance(); //静态的获取数据库对象的函数
//插入一个用户信息
bool insertUsr(QString name,QString pwd);
//根据name查找用户
bool findUsr(QString name);
//匹配name和pwd
bool match(QString name, QString pwd);
//删除用户信息
bool Delete(QString name);
DataBase();
QSqlDatabase db_;
};
#endif // DATABASE_H

23
datamanager.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "datamanager.h"
DataManager::DataManager(QObject *parent)
: QObject{parent}
{
database = DataBase::getInstance();
protocol = Protocol::getInstance();
server = TCPServer::getInstance();
server->configAndrun();
server->setCallBack(disconnect_from_cln);
server->setCallBack(recv_from_cln);
}
void DataManager::disconnect_from_cln(QTcpSocket *sock)
{
}
void DataManager::recv_from_cln(QByteArray &data, QTcpSocket *sock)
{
}

28
datamanager.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef DATAMANAGER_H
#define DATAMANAGER_H
#include <QObject>
#include "database.h"
#include "tcpserver.h"
#include "protocol.h"
class DataManager : public QObject
{
Q_OBJECT
public:
explicit DataManager(QObject *parent = nullptr);
static void disconnect_from_cln(QTcpSocket* sock);
static void recv_from_cln(QByteArray &data, QTcpSocket* sock);
signals:
private:
DataBase *database;
Protocol *protocol;
TCPServer *server;
QList <QTcpSocket*> online_socket_list;
QList <QString> online_name_list;
};
#endif // DATAMANAGER_H

10
main.cpp Normal file
View File

@ -0,0 +1,10 @@
#include <QCoreApplication>
#include <datamanager.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
DataManager datamanager;
return a.exec();
}

152
protocol.cpp Normal file
View File

@ -0,0 +1,152 @@
#include "protocol.h"
#include <QJsonDocument>
#include <QJsonArray>
Protocol Protocol::protocol_instance;
Protocol::Protocol(QObject *parent)
: QObject{parent}
{
}
Protocol::~Protocol()
{
}
Protocol* Protocol::getInstance()
{
return &protocol_instance;
}
QByteArray Protocol::data_builder(QList<any_types> &args)
{
QJsonObject data;
switch(args[0].get_data_types())
{
case MESSAGE:
{
if (args.length()==2)
{
data.insert("type","message");
data.insert("data",args[1].get_string());
}
else if (args.length()==3)
{
data.insert("type","message");
data.insert("to_user",args[1].get_string());
data.insert("data",args[2].get_string());
}
break;
}
case FILE:
{
data.insert("type","file");
data.insert("name",args[1].get_string());
data.insert("data",args[2].get_string());
break;
}
case REQUEST:
{
data.insert("type","request");
data.insert("action",args[1].get_string());
QList<QString> content = args[2].get_list();
QJsonObject json_content;
for (int i=0; i<content.length(); i+=2)
json_content.insert(content[i],content[i+1]);
data.insert("content",json_content);
break;
}
case ONLINEUSERS:
{
data.insert("type","online_users");
data.insert("count",args[1].get_int());
QList<QString> users = args[2].get_list();
QJsonArray json_users;
for (auto &user:users)
json_users.append(user);
data.insert("users",json_users);
break;
}
case RESPONSE:
{
data.insert("type","response");
data.insert("action",args[1].get_string());
QList<QString> content = args[2].get_list();
QJsonObject json_content;
for (int i=0; i<content.length(); i+=2)
json_content.insert(content[i],content[i+1]);
data.insert("content",json_content);
break;
}
default:
break;
}
QJsonDocument data_doc(data);
return data_doc.toJson(QJsonDocument::Compact)+'\n';
}
QPair<Protocol::data_types,QJsonObject> Protocol::data_parser(QByteArray data)
{
QJsonDocument json_data = QJsonDocument::fromJson(data);
if (json_data["type"] == "message")
return qMakePair(MESSAGE,json_data.object());
else if (json_data["type"] == "file")
return qMakePair(FILE,json_data.object());
else if (json_data["type"] == "request")
return qMakePair(REQUEST,json_data.object());
else if (json_data["type"] == "online_users")
return qMakePair(ONLINEUSERS,json_data.object());
else if (json_data["type"] == "response")
return qMakePair(RESPONSE,json_data.object());
else
return qMakePair(RESPONSE,json_data.object());
}
QByteArray Protocol::XOR_En_Decrypt(QString src)
{
QByteArray result;
for(auto &i:src)
result.append(i.toLatin1() ^ cipher_word);
return result;
}
QByteArray Protocol::Upper_Lower_En_Decrypt(QString src)
{
QByteArray result;
for(auto &i:src)
{
if(i.isUpper())
{
QChar word = i.toLower();
result.append(word.toLatin1());
}
else if (i.isLower())
{
QChar word = i.toUpper();
result.append(word.toLatin1());
}
else
result.append(i.toLatin1());
}
return result;
}
QByteArray Protocol::data_encrypt(QByteArray src)
{
QByteArray en_base64 = src.toBase64();
QByteArray result = Upper_Lower_En_Decrypt(en_base64);
return XOR_En_Decrypt(result);
}
QByteArray Protocol::data_decrypt(QByteArray src)
{
QByteArray result = XOR_En_Decrypt(src);
result = Upper_Lower_En_Decrypt(result);
return QByteArray::fromBase64(result);
}
void Protocol::set_data_cipher_word(char word)
{
cipher_word = word;
}

60
protocol.h Normal file
View File

@ -0,0 +1,60 @@
#ifndef PROTOCOL_H
#define PROTOCOL_H
#include <QObject>
#include <QJsonObject>
class Protocol : public QObject
{
Q_OBJECT
public:
static Protocol* getInstance();
template<typename Type, typename ...Args>
QByteArray data_maker(Type type, Args ...args)
{
QList<any_types> args_list;
args_list.append(any_types(type));
(void)std::initializer_list <int> {(args_list.append(any_types(args)),0)...};
return data_builder(args_list);
};
enum data_types {MESSAGE,FILE,REQUEST,ONLINEUSERS,RESPONSE};
QPair<data_types,QJsonObject> data_parser(QByteArray data);
QByteArray data_encrypt(QByteArray src);
void set_data_cipher_word(char word);
QByteArray data_decrypt(QByteArray src);
private:
char cipher_word = 'G';
struct any_types {
enum type {String, Bool, Int, Data_types, List};
any_types(int e) { m_data.INT = e; m_type = Int;}
any_types(QString e) { m_data.STRING = e; m_type = String;}
any_types(QList<QString> e) { m_data.LIST = e; m_type = List;}
any_types(bool e) { m_data.BOOL = e; m_type = Bool;}
any_types(data_types e) { m_data.DATA_TYPES = e; m_type = Data_types;}
type get_type() const { return m_type; }
int get_int() const { return m_data.INT; }
bool get_bool() const { return m_data.BOOL; }
data_types get_data_types() const { return m_data.DATA_TYPES; }
QString get_string() const { return m_data.STRING; }
QList<QString> get_list() const { return m_data.LIST; }
private:
type m_type;
struct {
int INT;
bool BOOL;
QString STRING;
QList<QString> LIST;
data_types DATA_TYPES;
} m_data;
};
static Protocol protocol_instance;
explicit Protocol(QObject *parent = nullptr);
~Protocol();
QByteArray data_builder(QList<any_types> &args);
QByteArray XOR_En_Decrypt(QString src);
QByteArray Upper_Lower_En_Decrypt(QString src);
signals:
};
#endif // PROTOCOL_H

124
tcpserver.cpp Normal file
View File

@ -0,0 +1,124 @@
#include "tcpserver.h"
using namespace std;
TCPServer TCPServer::tcpserver_instance;
TCPServer::TCPServer(QObject *parent)
: QObject{parent}
{
}
TCPServer::~TCPServer()
{
}
TCPServer* TCPServer::getInstance()
{
return &tcpserver_instance;
}
void TCPServer::configAndrun()
{
tcp_server.listen(QHostAddress(listen_addr), listen_port);
connect(&tcp_server, &QTcpServer::newConnection, this, &TCPServer::acceptTCPConnection);
QByteArray addr = listen_addr.toLocal8Bit();
qDebug() << "successfully start server";
printf("TCP Server Started at %.*s:%d.\n", addr.length(), addr.data(),listen_port);
}
void TCPServer::setServer(QString IP, qint16 port)
{
listen_addr = IP;
listen_port = port;
}
void TCPServer::stopRun()
{
tcp_server.close();
QByteArray addr = listen_addr.toLocal8Bit();
printf("TCP Server Closed at %.*s:%d.\n",addr.length(), addr.data(),listen_port);
}
void TCPServer::acceptTCPConnection()
{
QTcpSocket* clientConnection = tcp_server.nextPendingConnection();
if(clientConnection!=nullptr)
{
qDebug() << "new conncection";
clientConnection->setParent(this);
tcp_socket_list.append(clientConnection);
connect(clientConnection, &QTcpSocket::readyRead, this, &TCPServer::TCPReadPeer);
connect(clientConnection,&QTcpSocket::disconnected,this,&TCPServer::discTCPConnection);
QString ip = clientConnection->peerAddress().toString();
string ip_t = ip.toStdString();
printf("TCP: %.*s:%d connected.\n",ip_t.length(), ip_t.data(), clientConnection->peerPort());
}
}
void TCPServer::discTCPConnection()
{
for (int i=0;i<tcp_socket_list.length();i++)
{
if (tcp_socket_list[i] == dynamic_cast<QTcpSocket*>(sender()))//可以直接用sender()解决,此处为稳健起见还是采取遍历
{
QString ip = tcp_socket_list[i]->peerAddress().toString();
string ip_t = ip.toStdString();
printf("TCP: %.*s:%d Disconnected.\n",ip_t.length(), ip_t.data(), tcp_socket_list[i]->peerPort());
if (disccallback != nullptr)
disccallback(tcp_socket_list[i]);
tcp_socket_list.removeAt(i);
dynamic_cast<QTcpSocket*>(sender())->deleteLater();
qDebug() << "disconnect";
break;
}
}
}
void TCPServer::TCPReadPeer()
{
sock =(QTcpSocket *)sender();
QByteArray recv;
while(sock->bytesAvailable())//循环接收
{
recv += sock->readAll();
if (!sock->waitForReadyRead(20))//超时停止接收
{
/*QString error = sock->errorString();
string error_t = error.toStdString();
log_w("error:%d: %.*s", sock->error(),error_t.length(), error_t.data());*/
break;
}
}
QString ip = sock->peerAddress().toString();
string ip_t = ip.toStdString();
printf("TCPC: Recvd from %.*s:%d\n", ip_t.length(), ip_t.data(), sock->peerPort());
printf("TCP Message:%.*s\n",recv.size(),(char *)recv.data());
if (recvcallback != nullptr)
recvcallback(recv,sock);
}
void TCPServer::setCallBack(void (*callback)(QByteArray &data, QTcpSocket* socket))
{
recvcallback = callback;
}
void TCPServer::setCallBack(void (*callback)(QTcpSocket* socket))
{
disccallback = callback;
}
void TCPServer::sendToclient(QString ip, quint16 port, QByteArray data)
{
QHostAddress addr(ip);
for (int i=0;i<tcp_socket_list.length();i++)
{
if (addr == tcp_socket_list[i]->peerAddress()&&tcp_socket_list[i]->peerPort()==port)
{
tcp_socket_list[i]->write(data);
break;
}
}
}

38
tcpserver.h Normal file
View File

@ -0,0 +1,38 @@
#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
#include <QDebug>
class TCPServer : public QObject
{
Q_OBJECT
public:
static TCPServer* getInstance();
void configAndrun();
void setServer(QString IP, qint16 port);
void setCallBack(void (*callback)(QByteArray &data,QTcpSocket* socket));//Qt的信号-槽机制实际就是回调函数,
void setCallBack(void (*callback)(QTcpSocket* socket)); //此处为练手,不使用该机制
void sendToclient(QString ip, quint16 port, QByteArray data);
void stopRun();
private:
explicit TCPServer(QObject *parent = nullptr);
~TCPServer();
QString listen_addr = "0.0.0.0";
qint16 listen_port = 7890;
QTcpServer tcp_server;
QTcpSocket* sock;
QList <QTcpSocket*> tcp_socket_list;
void (*recvcallback) (QByteArray &data, QTcpSocket* socket)=nullptr;
void (*disccallback) (QTcpSocket* socket)=nullptr;
static TCPServer tcpserver_instance;
private slots:
void acceptTCPConnection();
void TCPReadPeer();
void discTCPConnection();
signals:
};
#endif // TCPSERVER_H