Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- using BIF.SWE1.Interfaces;
- namespace MyWebServer
- {
- class HttpServer
- {
- private bool _running = false;
- private TcpListener _listener;
- public HttpServer(int port)
- {
- _listener = new TcpListener(IPAddress.Any, port);
- }
- public void Start()
- {
- Thread serverThread = new Thread(new ThreadStart(Run));
- serverThread.Start();
- }
- public void Run()
- {
- _running = true;
- _listener.Start();
- while (_running)
- {
- Console.WriteLine("Waiting for connection...");
- TcpClient client = _listener.AcceptTcpClient();
- HandleClient(client);
- client.Close();
- }
- _running = false;
- _listener.Stop();
- }
- private void HandleClient(TcpClient client)
- {
- Request req = new Request(client.GetStream());
- PluginManager m = new PluginManager();
- IPlugin plugin = m.GetRequestedPlugin(req);
- Response response = (Response)plugin.Handle(req);
- Console.WriteLine("response created");
- response.Send(client.GetStream());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement