From 28de1ebd2de942f0edb12e97313b5bb382009e13 Mon Sep 17 00:00:00 2001 From: class Date: Mon, 15 Apr 2024 20:12:04 +0300 Subject: [PATCH] HttpServer --- HttpProject.sln | 25 ++++++ HttpProject/App.config | 6 ++ HttpProject/HttpProject.csproj | 58 ++++++++++++++ HttpProject/Program.cs | 101 +++++++++++++++++++++++++ HttpProject/Properties/AssemblyInfo.cs | 36 +++++++++ 5 files changed, 226 insertions(+) create mode 100644 HttpProject.sln create mode 100644 HttpProject/App.config create mode 100644 HttpProject/HttpProject.csproj create mode 100644 HttpProject/Program.cs create mode 100644 HttpProject/Properties/AssemblyInfo.cs diff --git a/HttpProject.sln b/HttpProject.sln new file mode 100644 index 0000000..38db178 --- /dev/null +++ b/HttpProject.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.28803.352 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpProject", "HttpProject\HttpProject.csproj", "{BE5D0786-ACE4-40F1-8E54-3A6C5AFBA5C7}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BE5D0786-ACE4-40F1-8E54-3A6C5AFBA5C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BE5D0786-ACE4-40F1-8E54-3A6C5AFBA5C7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BE5D0786-ACE4-40F1-8E54-3A6C5AFBA5C7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BE5D0786-ACE4-40F1-8E54-3A6C5AFBA5C7}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {29CC3C79-BE5F-4BAC-A965-B05CD89AEFAE} + EndGlobalSection +EndGlobal diff --git a/HttpProject/App.config b/HttpProject/App.config new file mode 100644 index 0000000..56efbc7 --- /dev/null +++ b/HttpProject/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/HttpProject/HttpProject.csproj b/HttpProject/HttpProject.csproj new file mode 100644 index 0000000..4ef7b34 --- /dev/null +++ b/HttpProject/HttpProject.csproj @@ -0,0 +1,58 @@ + + + + + Debug + AnyCPU + {BE5D0786-ACE4-40F1-8E54-3A6C5AFBA5C7} + Exe + HttpProject + HttpProject + v4.7.2 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/HttpProject/Program.cs b/HttpProject/Program.cs new file mode 100644 index 0000000..643462e --- /dev/null +++ b/HttpProject/Program.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; +using System.IO; +using System.Net.Http; +using System.Threading; + +namespace HttpProject +{ + + class Program + { + + //static async Task Main(string[] args) + //{ + // var listener = new HttpListener(); + // listener.Prefixes.Add("http://localhost:8080/"); // + + // try + // { + // listener.Start(); + // Console.WriteLine("Сервер запущен. Ожидание подключений..."); + + // while (true) + // { + // var context = await listener.GetContextAsync(); + // HandleRequest(context); + // } + // } + // catch (Exception ex) + // { + // Console.WriteLine($"Ошибка: {ex.Message}"); + // } + // finally + // { + // listener.Close(); + // } + //} + + //static void HandleRequest(HttpListenerContext context) + //{ + // var request = context.Request; + // var response = context.Response; + + // Console.WriteLine($"Получен запрос: {request.HttpMethod} {request.Url}"); + + // string responseString = "

Hello, world!

"; + // byte[] buffer = Encoding.UTF8.GetBytes(responseString); + + // response.ContentType = "text/html"; + // response.ContentLength64 = buffer.Length; + + // using (Stream output = response.OutputStream) + // { + // output.Write(buffer, 0, buffer.Length); + // } + + // response.Close(); + //} + + public static void Main() + { + + Console.WriteLine("Starting echo server..."); + + + + int port = 1234; + TcpListener listener = new TcpListener(IPAddress.Any, port); + listener.Start(); + + + while (true) + { + TcpClient client = listener.AcceptTcpClient(); + NetworkStream stream = client.GetStream(); + //StreamWriter writer = new StreamWriter(stream, Encoding.ASCII) { AutoFlush = true }; + StreamReader reader = new StreamReader(stream, Encoding.UTF8); + + while (true) + { + string inputLine = ""; + while (inputLine != null) + { + inputLine = reader.ReadLine(); + //writer.WriteLine("Echoing string: " + inputLine); + Console.WriteLine("Echoing string: " + inputLine); + } + Console.WriteLine("Server saw disconnect from client."); + } + } + } + + } + + +} \ No newline at end of file diff --git a/HttpProject/Properties/AssemblyInfo.cs b/HttpProject/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..e866365 --- /dev/null +++ b/HttpProject/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Общие сведения об этой сборке предоставляются следующим набором +// набора атрибутов. Измените значения этих атрибутов для изменения сведений, +// связанные с этой сборкой. +[assembly: AssemblyTitle("HttpProject")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("HttpProject")] +[assembly: AssemblyCopyright("Copyright © 2024")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Установка значения False для параметра ComVisible делает типы в этой сборке невидимыми +// для компонентов COM. Если необходимо обратиться к типу в этой сборке через +// из модели COM задайте для атрибута ComVisible этого типа значение true. +[assembly: ComVisible(false)] + +// Следующий GUID представляет идентификатор typelib, если этот проект доступен из модели COM +[assembly: Guid("be5d0786-ace4-40f1-8e54-3a6c5afba5c7")] + +// Сведения о версии сборки состоят из указанных ниже четырех значений: +// +// Основной номер версии +// Дополнительный номер версии +// Номер сборки +// Номер редакции +// +// Можно задать все значения или принять номера сборки и редакции по умолчанию +// используя "*", как показано ниже: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")]