-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
116 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters