From dc5651e00bb5a913227cc7a1e6883f5fbe2358cb Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Tue, 9 Nov 2021 17:30:06 +0300 Subject: [PATCH 1/2] test --- Test1.1/Test1.1.sln | 22 ++++++++++ Test1.1/Test1.1/Client.cs | 60 +++++++++++++++++++++++++++ Test1.1/Test1.1/Dockerfile | 18 ++++++++ Test1.1/Test1.1/Program.cs | 22 ++++++++++ Test1.1/Test1.1/Server.cs | 76 ++++++++++++++++++++++++++++++++++ Test1.1/Test1.1/Test1.1.csproj | 10 +++++ 6 files changed, 208 insertions(+) create mode 100644 Test1.1/Test1.1.sln create mode 100644 Test1.1/Test1.1/Client.cs create mode 100644 Test1.1/Test1.1/Dockerfile create mode 100644 Test1.1/Test1.1/Program.cs create mode 100644 Test1.1/Test1.1/Server.cs create mode 100644 Test1.1/Test1.1/Test1.1.csproj diff --git a/Test1.1/Test1.1.sln b/Test1.1/Test1.1.sln new file mode 100644 index 0000000..efdec28 --- /dev/null +++ b/Test1.1/Test1.1.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test1.1", "Test1.1\Test1.1.csproj", "{E451040C-44C5-427F-9CF7-F83F90E54DFF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test1.1.Tests", "Test1.1.Tests\Test1.1.Tests.csproj", "{E628CC17-DD0A-4658-8561-CB4C496A798D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E451040C-44C5-427F-9CF7-F83F90E54DFF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E451040C-44C5-427F-9CF7-F83F90E54DFF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E451040C-44C5-427F-9CF7-F83F90E54DFF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E451040C-44C5-427F-9CF7-F83F90E54DFF}.Release|Any CPU.Build.0 = Release|Any CPU + {E628CC17-DD0A-4658-8561-CB4C496A798D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E628CC17-DD0A-4658-8561-CB4C496A798D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E628CC17-DD0A-4658-8561-CB4C496A798D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E628CC17-DD0A-4658-8561-CB4C496A798D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Test1.1/Test1.1/Client.cs b/Test1.1/Test1.1/Client.cs new file mode 100644 index 0000000..cfbb4ac --- /dev/null +++ b/Test1.1/Test1.1/Client.cs @@ -0,0 +1,60 @@ +using System; +using System.IO; +using System.Net.Sockets; +using System.Threading.Tasks; + +namespace Test1._1 +{ + /// + /// класс клиента + /// + public class Client + { + private TcpClient _client; + + public Client(string host, int port) + { + _client = new TcpClient(host, port); + } + + public void Working() + { + while (true) + { + Task.Run(async () => + { + while (true) { + await Writer(_client.GetStream()); + await Reader(_client.GetStream()); + } + }); + } + } + + private async Task Writer(NetworkStream stream) + { + await Task.Run(async () => + { + var writer = new StreamWriter(stream) { AutoFlush = true }; + while (true) + { + var data = Console.ReadLine(); + await writer.WriteAsync(data + "\n"); + } + }); + } + + private async Task Reader(NetworkStream stream) + { + await Task.Run(async () => + { + var reader = new StreamReader(stream); + while (true) + { + var data = await reader.ReadLineAsync(); + Console.WriteLine(data + "\n"); + } + }); + } + } +} \ No newline at end of file diff --git a/Test1.1/Test1.1/Dockerfile b/Test1.1/Test1.1/Dockerfile new file mode 100644 index 0000000..695ecdc --- /dev/null +++ b/Test1.1/Test1.1/Dockerfile @@ -0,0 +1,18 @@ +FROM mcr.microsoft.com/dotnet/runtime:5.0 AS base +WORKDIR /app + +FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build +WORKDIR /src +COPY ["Test1.1/Test1.1.csproj", "Test1.1/"] +RUN dotnet restore "Test1.1/Test1.1.csproj" +COPY . . +WORKDIR "/src/Test1.1" +RUN dotnet build "Test1.1.csproj" -c Release -o /app/build + +FROM build AS publish +RUN dotnet publish "Test1.1.csproj" -c Release -o /app/publish + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "Test1.1.dll"] diff --git a/Test1.1/Test1.1/Program.cs b/Test1.1/Test1.1/Program.cs new file mode 100644 index 0000000..efa6b8d --- /dev/null +++ b/Test1.1/Test1.1/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Threading.Tasks; + +namespace Test1._1 +{ + class Program + { + static async Task Main(string[] args) + { + if (args.Length == 1) + { + var server = new Server(Int32.Parse(args[0])); + await server.Working(); + } + else + { + var client = new Client(args[0], Int32.Parse(args[1])); + client.Working(); + } + } + } +} \ No newline at end of file diff --git a/Test1.1/Test1.1/Server.cs b/Test1.1/Test1.1/Server.cs new file mode 100644 index 0000000..c3cbfda --- /dev/null +++ b/Test1.1/Test1.1/Server.cs @@ -0,0 +1,76 @@ +using System; +using System.IO; +using System.Net; +using System.Net.Sockets; +using System.Threading; +using System.Threading.Tasks; + +namespace Test1._1 +{ + /// + /// класс серва + /// + public class Server + { + private int _port; + private readonly CancellationTokenSource _cancellationToken = new CancellationTokenSource(); + public Server(int port) + { + _port = port; + } + + /// + /// метод в котором происходит отправка и принятие сообщение + /// + public async Task Working() + { + var listener = new TcpListener(IPAddress.Any, _port); + listener.Start(); + var client = await listener.AcceptTcpClientAsync(); + while (!_cancellationToken.IsCancellationRequested) + { + await Task.Run(async () => + { + while (true) { + await Writer(client.GetStream()); + await Reader(client.GetStream()); + } + }); + } + listener.Stop(); + } + + private async Task Writer(NetworkStream stream) + { + await Task.Run(async () => + { + var writer = new StreamWriter(stream) { AutoFlush = true }; + while (true) + { + var data = Console.ReadLine(); + await writer.WriteAsync(data + "\n"); + } + }); + } + + private async Task Reader(NetworkStream stream) + { + await Task.Run(async () => + { + var reader = new StreamReader(stream); + while (true) + { + var data = await reader.ReadLineAsync(); + Console.WriteLine(data + "\n"); + if (data == "exit") + { + StopServer(); + } + } + }); + } + + private void StopServer() + => _cancellationToken.Cancel(); + } +} \ No newline at end of file diff --git a/Test1.1/Test1.1/Test1.1.csproj b/Test1.1/Test1.1/Test1.1.csproj new file mode 100644 index 0000000..483c692 --- /dev/null +++ b/Test1.1/Test1.1/Test1.1.csproj @@ -0,0 +1,10 @@ + + + + Exe + net5.0 + Test1._1 + Windows + + + From eb0970697f2b97c57c4c99b9cf8122434e074a32 Mon Sep 17 00:00:00 2001 From: MikePuzanov Date: Mon, 22 Nov 2021 10:47:48 +0300 Subject: [PATCH 2/2] . --- Test1.1/Test1.1/.dockerignore | 25 +++++++++++++++++++++++++ Test1.1/Test1.1/Client.cs | 4 ++-- Test1.1/Test1.1/Program.cs | 2 +- 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 Test1.1/Test1.1/.dockerignore diff --git a/Test1.1/Test1.1/.dockerignore b/Test1.1/Test1.1/.dockerignore new file mode 100644 index 0000000..cd967fc --- /dev/null +++ b/Test1.1/Test1.1/.dockerignore @@ -0,0 +1,25 @@ +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.idea +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md \ No newline at end of file diff --git a/Test1.1/Test1.1/Client.cs b/Test1.1/Test1.1/Client.cs index cfbb4ac..5203365 100644 --- a/Test1.1/Test1.1/Client.cs +++ b/Test1.1/Test1.1/Client.cs @@ -17,11 +17,11 @@ public Client(string host, int port) _client = new TcpClient(host, port); } - public void Working() + public async Task Working() { while (true) { - Task.Run(async () => + await Task.Run(async () => { while (true) { await Writer(_client.GetStream()); diff --git a/Test1.1/Test1.1/Program.cs b/Test1.1/Test1.1/Program.cs index efa6b8d..a29655d 100644 --- a/Test1.1/Test1.1/Program.cs +++ b/Test1.1/Test1.1/Program.cs @@ -15,7 +15,7 @@ static async Task Main(string[] args) else { var client = new Client(args[0], Int32.Parse(args[1])); - client.Working(); + await client.Working(); } } }