Skip to content

Commit

Permalink
添加异步tcp client示例
Browse files Browse the repository at this point in the history
  • Loading branch information
beetlex-io committed Jan 11, 2020
1 parent 26ecb5f commit b6fbb8a
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
12 changes: 12 additions & 0 deletions TCP.AsynClient/Client/Client.csproj
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BeetleX" Version="1.4.8.3" />
</ItemGroup>

</Project>
34 changes: 34 additions & 0 deletions TCP.AsynClient/Client/Program.cs
@@ -0,0 +1,34 @@
using BeetleX;
using BeetleX.Clients;
using System;
using System.Net;

namespace Client
{
class Program
{
static void Main(string[] args)
{
AsyncTcpClient client = SocketFactory.CreateClient<AsyncTcpClient>("127.0.0.1", 9090);
client.DataReceive = (o, e) =>
{
var pipestream = e.Stream.ToPipeStream();
if (pipestream.TryReadLine(out string line))
{
Console.WriteLine(line);
}
};
while (true)
{
Console.Write("Enter Name:");
BytesHandler line = Console.ReadLine() + "\r\n";
client.Send(line);
if(!client.IsConnected)
{
Console.WriteLine(client.LastError.Message);
}
}

}
}
}
30 changes: 30 additions & 0 deletions TCP.AsynClient/Server/Program.cs
@@ -0,0 +1,30 @@
using BeetleX;
using BeetleX.EventArgs;
using System;

namespace Server
{
class Program : ServerHandlerBase
{
private static IServer server;
public static void Main(string[] args)
{
server = SocketFactory.CreateTcpServer<Program>();
//server.Options.DefaultListen.Port =9090;
//server.Options.DefaultListen.Host = "127.0.0.1";
server.Open();
Console.Read();
}
public override void SessionReceive(IServer server, SessionReceiveEventArgs e)
{
var pipeStream = e.Stream.ToPipeStream();
if (pipeStream.TryReadLine(out string name))
{
Console.WriteLine(name);
e.Session.Stream.ToPipeStream().WriteLine("hello " + name);
e.Session.Stream.Flush();
}
base.SessionReceive(server, e);
}
}
}
12 changes: 12 additions & 0 deletions TCP.AsynClient/Server/Server.csproj
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BeetleX" Version="1.4.8.3" />
</ItemGroup>

</Project>
31 changes: 31 additions & 0 deletions TCP.AsynClient/TCP.AsynClient.sln
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.705
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Server", "Server\Server.csproj", "{01636360-991A-43DE-8C32-78C00D5C7406}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "Client\Client.csproj", "{7C8A6B33-57CF-4FA0-B855-E6411C378194}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{01636360-991A-43DE-8C32-78C00D5C7406}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{01636360-991A-43DE-8C32-78C00D5C7406}.Debug|Any CPU.Build.0 = Debug|Any CPU
{01636360-991A-43DE-8C32-78C00D5C7406}.Release|Any CPU.ActiveCfg = Release|Any CPU
{01636360-991A-43DE-8C32-78C00D5C7406}.Release|Any CPU.Build.0 = Release|Any CPU
{7C8A6B33-57CF-4FA0-B855-E6411C378194}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7C8A6B33-57CF-4FA0-B855-E6411C378194}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7C8A6B33-57CF-4FA0-B855-E6411C378194}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7C8A6B33-57CF-4FA0-B855-E6411C378194}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {A2A9DF2D-E003-4BEB-A5F3-B3B46279390B}
EndGlobalSection
EndGlobal

0 comments on commit b6fbb8a

Please sign in to comment.