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/.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 new file mode 100644 index 0000000..5203365 --- /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 async Task Working() + { + while (true) + { + await 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..a29655d --- /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])); + await 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 + + +