101 lines
2.9 KiB
C#
101 lines
2.9 KiB
C#
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 = "<html><body><h1>Hello, world!</h1></body></html>";
|
|
// 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.");
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
} |