Skip to content

Commit

Permalink
增加网络客户端与服务端例程
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Oct 27, 2023
1 parent 363f017 commit 6f5e787
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
<FileVersion>$(Version)</FileVersion>
<AssemblyVersion>$(VersionPrefix).*</AssemblyVersion>
<Deterministic>false</Deterministic>
<OutputPath>..\..\Bin\KeyTest</OutputPath>
<OutputPath>..\..\Bin\NetClientTest</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NewLife.Core" Version="10.6.2023.1001" />
</ItemGroup>

</Project>
34 changes: 34 additions & 0 deletions Samples/NetClientTest/Program.cs
Original file line number Diff line number Diff line change
@@ -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);
}
24 changes: 24 additions & 0 deletions Samples/NetServerTest/NetServerTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<Company>新生命开发团队</Company>
<Copyright>©2002-2023 新生命开发团队</Copyright>
<VersionPrefix>1.0</VersionPrefix>
<VersionSuffix>$([System.DateTime]::Now.ToString(`yyyy.MMdd`))</VersionSuffix>
<Version>$(VersionPrefix).$(VersionSuffix)</Version>
<FileVersion>$(Version)</FileVersion>
<AssemblyVersion>$(VersionPrefix).*</AssemblyVersion>
<Deterministic>false</Deterministic>
<OutputPath>..\..\Bin\NetServerTest</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NewLife.Core" Version="10.6.2023.1001" />
</ItemGroup>

</Project>
39 changes: 39 additions & 0 deletions Samples/NetServerTest/Program.cs
Original file line number Diff line number Diff line change
@@ -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();
16 changes: 0 additions & 16 deletions Samples/NetTest/Program.cs

This file was deleted.

21 changes: 14 additions & 7 deletions SmartA2.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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}
Expand Down

0 comments on commit 6f5e787

Please sign in to comment.