tap (task-based asynchronous pattern)

This commit is contained in:
2023-02-07 14:44:58 +09:00
parent cb8fba64b1
commit f74090a49e
7 changed files with 142 additions and 0 deletions

24
TAP/Client/Client.cs Normal file
View File

@@ -0,0 +1,24 @@
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Client
{
internal class Client
{
static async Task Main(string[] args)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 20000);
await socket.ConnectAsync(endPoint);
while (true)
{
string str = Console.ReadLine() ?? string.Empty;
byte[] buffer = Encoding.UTF8.GetBytes(str);
await socket.SendAsync(buffer, SocketFlags.None);
}
}
}
}

10
TAP/Client/Client.csproj Normal file
View File

@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

47
TAP/Server/Server.cs Normal file
View File

@@ -0,0 +1,47 @@
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Server
{
internal class Server
{
private static readonly int BUFFER_SIZE = 256;
static async Task Main(string[] args)
{
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 20000);
serverSocket.Bind(endPoint);
serverSocket.Listen(1000);
while (true)
{
Socket clientSocket = await serverSocket.AcceptAsync();
Console.WriteLine(clientSocket.RemoteEndPoint);
ThreadPool.QueueUserWorkItem(ReadAsync, clientSocket);
}
}
private static async void ReadAsync(object? state)
{
if (state == null)
return;
Socket clientSocket = (Socket)state;
while (true)
{
byte[] buffer = new byte[BUFFER_SIZE];
int n1 = await clientSocket.ReceiveAsync(buffer, SocketFlags.None);
if (n1 < 1)
{
Console.WriteLine("Client is disconnected...");
clientSocket.Dispose();
return;
}
Console.WriteLine(Encoding.UTF8.GetString(buffer));
}
}
}
}

10
TAP/Server/Server.csproj Normal file
View File

@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

31
TAP/TAP.sln Normal file
View File

@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.3.32929.385
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server", "Server\Server.csproj", "{BE7AA4FD-6859-4010-A6A4-991A7E5BA6AB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client", "Client\Client.csproj", "{F632989E-390D-4098-9114-F1BCE43B8B82}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BE7AA4FD-6859-4010-A6A4-991A7E5BA6AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BE7AA4FD-6859-4010-A6A4-991A7E5BA6AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BE7AA4FD-6859-4010-A6A4-991A7E5BA6AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BE7AA4FD-6859-4010-A6A4-991A7E5BA6AB}.Release|Any CPU.Build.0 = Release|Any CPU
{F632989E-390D-4098-9114-F1BCE43B8B82}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F632989E-390D-4098-9114-F1BCE43B8B82}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F632989E-390D-4098-9114-F1BCE43B8B82}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F632989E-390D-4098-9114-F1BCE43B8B82}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9E9FD3BE-FDB5-4256-AC8D-B528D1B5A9BE}
EndGlobalSection
EndGlobal

10
TAP/TAP/Program.cs Normal file
View File

@@ -0,0 +1,10 @@
namespace TAP
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}

10
TAP/TAP/TAP.csproj Normal file
View File

@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>