غير مصنف
http server
using System;
using System.Net;
using System.IO;
public class HttpServer
{
public int Port = 8081;
private HttpListener _listener;
public void Start()
{
_listener = new HttpListener();
_listener.Prefixes.Add("http://*:" + Port.ToString() + "/");
_listener.Start();
Receive();
}
public void Stop()
{
_listener.Stop();
}
private void Receive()
{
_listener.BeginGetContext(new AsyncCallback(ListenerCallback), _listener);
}
static public int max = 5;
private void ListenerCallback(IAsyncResult result)
{
try
{
if (_listener.IsListening)
{
var context = _listener.EndGetContext(result);
var request = context.Request;
// do something with the request
Console.WriteLine($"{request.Url}");
max--;
Receive();
}
}
catch { }
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting HTTP listener...");
while (true)
{
HttpServer.max = 5;
var httpServer = new HttpServer();
httpServer.Start();
while (HttpServer.max > 1) ;
httpServer.Stop();
}
Console.WriteLine("Exiting gracefully...");
}
}