From 6f5e787d25604040bd8daf51cb1417ea5f4e3d8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=BA=E8=83=BD=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Sat, 28 Oct 2023 00:18:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BD=91=E7=BB=9C=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E4=B8=8E=E6=9C=8D=E5=8A=A1=E7=AB=AF=E4=BE=8B?= =?UTF-8?q?=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NetClientTest.csproj} | 6 ++- Samples/NetClientTest/Program.cs | 34 ++++++++++++++++ Samples/NetServerTest/NetServerTest.csproj | 24 ++++++++++++ Samples/NetServerTest/Program.cs | 39 +++++++++++++++++++ Samples/NetTest/Program.cs | 16 -------- SmartA2.sln | 21 ++++++---- 6 files changed, 116 insertions(+), 24 deletions(-) rename Samples/{NetTest/NetTest.csproj => NetClientTest/NetClientTest.csproj} (82%) create mode 100644 Samples/NetClientTest/Program.cs create mode 100644 Samples/NetServerTest/NetServerTest.csproj create mode 100644 Samples/NetServerTest/Program.cs delete mode 100644 Samples/NetTest/Program.cs diff --git a/Samples/NetTest/NetTest.csproj b/Samples/NetClientTest/NetClientTest.csproj similarity index 82% rename from Samples/NetTest/NetTest.csproj rename to Samples/NetClientTest/NetClientTest.csproj index c88657f..072f3b3 100644 --- a/Samples/NetTest/NetTest.csproj +++ b/Samples/NetClientTest/NetClientTest.csproj @@ -11,10 +11,14 @@ $(Version) $(VersionPrefix).* false - ..\..\Bin\KeyTest + ..\..\Bin\NetClientTest false enable enable + + + + diff --git a/Samples/NetClientTest/Program.cs b/Samples/NetClientTest/Program.cs new file mode 100644 index 0000000..d6b7edf --- /dev/null +++ b/Samples/NetClientTest/Program.cs @@ -0,0 +1,34 @@ +using NewLife; +using NewLife.Log; +using NewLife.Net; + +// 网络客户端,一般跑在工控机上,充当硬件设备到服务器之间的桥梁 + +XTrace.UseConsole(); + +// 支持tcp/udp地址 +XTrace.WriteLine("请输入要连接的服务器:"); +var server = Console.ReadLine(); +if (server.IsNullOrEmpty()) server = "tcp://10.0.2.6:777"; + +var uri = new NetUri(server); +var client = uri.CreateRemote(); +client.Log = XTrace.Log; +client.LogSend = true; +client.LogReceive = true; + +// 在事件中接收数据 +client.Received += (s, e) => +{ + XTrace.WriteLine("收到数据:{0}", e.Packet.ToStr()); +}; +client.Open(); + +// 发送数据 +for (var i = 0; i < 10; i++) +{ + XTrace.WriteLine("请输入要发送的数据:"); + var input = Console.ReadLine(); + + client.Send(input); +} diff --git a/Samples/NetServerTest/NetServerTest.csproj b/Samples/NetServerTest/NetServerTest.csproj new file mode 100644 index 0000000..b7dfd9d --- /dev/null +++ b/Samples/NetServerTest/NetServerTest.csproj @@ -0,0 +1,24 @@ + + + + Exe + net7.0 + 新生命开发团队 + ©2002-2023 新生命开发团队 + 1.0 + $([System.DateTime]::Now.ToString(`yyyy.MMdd`)) + $(VersionPrefix).$(VersionSuffix) + $(Version) + $(VersionPrefix).* + false + ..\..\Bin\NetServerTest + false + enable + enable + + + + + + + diff --git a/Samples/NetServerTest/Program.cs b/Samples/NetServerTest/Program.cs new file mode 100644 index 0000000..eda299d --- /dev/null +++ b/Samples/NetServerTest/Program.cs @@ -0,0 +1,39 @@ +using NewLife.Log; +using NewLife.Net; + +// 网络服务端,一般跑在服务器或上位机上,用于接收工控机内客户端的数据 + +XTrace.UseConsole(); + +var server = new NetServer(777) +{ + Log = XTrace.Log, + SessionLog = XTrace.Log +}; + +// 新连接会话事件 +server.NewSession += (s, e) => +{ + var uri = e.Session.Remote; + XTrace.WriteLine("新会话:{0}", uri); + + var session = e.Session; + session.Send($"欢迎:{uri}"); +}; + +// 在事件中接收数据 +server.Received += (s, e) => +{ + var msg = e.Packet.ToStr(); + XTrace.WriteLine("收到数据:{0}", msg); + + // 倒序返回 + var session = s as INetSession; + var cs = msg.Reverse().ToArray(); + session.Send(new String(cs)); +}; + +server.Start(); + +// 等待退出 +Console.ReadLine(); \ No newline at end of file diff --git a/Samples/NetTest/Program.cs b/Samples/NetTest/Program.cs deleted file mode 100644 index d3822a9..0000000 --- a/Samples/NetTest/Program.cs +++ /dev/null @@ -1,16 +0,0 @@ -using SmartA2; - -var key = new InputPort { FileName = "/dev/key" }; - -var f = false; -for (var i = 0; i < 100; i++) -{ - var rs = key.Read(); - if (rs != f) - { - f = rs; - Console.WriteLine(f ? "按下" : "松开"); - } - - Thread.Sleep(100); -} \ No newline at end of file diff --git a/SmartA2.sln b/SmartA2.sln index 940be7a..990384a 100644 --- a/SmartA2.sln +++ b/SmartA2.sln @@ -42,10 +42,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "6.网络", "6.网络", "{43 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SerialTest", "Samples\SerialTest\SerialTest.csproj", "{65D7E24B-FAA3-4159-825F-8EE25316D52C}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetTest", "Samples\NetTest\NetTest.csproj", "{25809B61-CED1-4D7B-82B4-E2E3FC462A74}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "7.数据库", "7.数据库", "{C1A2B597-61C1-4A4B-AF34-A6E808B89522}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetClientTest", "Samples\NetClientTest\NetClientTest.csproj", "{A005D70E-BEC9-473C-AE26-F7C4217C5DC2}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetServerTest", "Samples\NetServerTest\NetServerTest.csproj", "{3BC6E35A-A5E0-41E5-A8E8-3822BBAF22BD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -84,10 +86,14 @@ Global {65D7E24B-FAA3-4159-825F-8EE25316D52C}.Debug|Any CPU.Build.0 = Debug|Any CPU {65D7E24B-FAA3-4159-825F-8EE25316D52C}.Release|Any CPU.ActiveCfg = Release|Any CPU {65D7E24B-FAA3-4159-825F-8EE25316D52C}.Release|Any CPU.Build.0 = Release|Any CPU - {25809B61-CED1-4D7B-82B4-E2E3FC462A74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {25809B61-CED1-4D7B-82B4-E2E3FC462A74}.Debug|Any CPU.Build.0 = Debug|Any CPU - {25809B61-CED1-4D7B-82B4-E2E3FC462A74}.Release|Any CPU.ActiveCfg = Release|Any CPU - {25809B61-CED1-4D7B-82B4-E2E3FC462A74}.Release|Any CPU.Build.0 = Release|Any CPU + {A005D70E-BEC9-473C-AE26-F7C4217C5DC2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A005D70E-BEC9-473C-AE26-F7C4217C5DC2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A005D70E-BEC9-473C-AE26-F7C4217C5DC2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A005D70E-BEC9-473C-AE26-F7C4217C5DC2}.Release|Any CPU.Build.0 = Release|Any CPU + {3BC6E35A-A5E0-41E5-A8E8-3822BBAF22BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3BC6E35A-A5E0-41E5-A8E8-3822BBAF22BD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3BC6E35A-A5E0-41E5-A8E8-3822BBAF22BD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3BC6E35A-A5E0-41E5-A8E8-3822BBAF22BD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -104,8 +110,9 @@ Global {50F09D8D-EE42-467C-A366-E50676F157E0} = {3DB3FAF5-8F1A-44EB-A407-025A4AB19AB0} {43CA44E0-92E8-4448-89A4-240AD34494A4} = {3DB3FAF5-8F1A-44EB-A407-025A4AB19AB0} {65D7E24B-FAA3-4159-825F-8EE25316D52C} = {50F09D8D-EE42-467C-A366-E50676F157E0} - {25809B61-CED1-4D7B-82B4-E2E3FC462A74} = {43CA44E0-92E8-4448-89A4-240AD34494A4} {C1A2B597-61C1-4A4B-AF34-A6E808B89522} = {3DB3FAF5-8F1A-44EB-A407-025A4AB19AB0} + {A005D70E-BEC9-473C-AE26-F7C4217C5DC2} = {43CA44E0-92E8-4448-89A4-240AD34494A4} + {3BC6E35A-A5E0-41E5-A8E8-3822BBAF22BD} = {43CA44E0-92E8-4448-89A4-240AD34494A4} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {323831A1-A95B-40AB-B9AD-36A0BC10C2CB}