Guest User

SERVER C#

a guest
Mar 18th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.14 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. using System.Collections;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Threading;
  8.  
  9. namespace HTTPServer
  10. {
  11.     public class Interaction
  12.     {
  13.         const string Server_Directory = "C:\\HTTPServer\\";
  14.         const string Error_Message = "None";
  15.         const string Main_Page = "nsu.htm";
  16.  
  17.         TcpClient Client;
  18.         Hashtable Contents = new Hashtable();
  19.         public string GetPath(string request)
  20.         {
  21.             int space1 = request.IndexOf(" ");
  22.             int space2 = request.IndexOf(" ", space1 + 1);
  23.             string url = request.Substring(space1 + 2, space2 - space1 - 2);
  24.             if (url == "")
  25.                 url = Main_Page;
  26.             return Server_Directory + url;
  27.         }
  28.         public string GetContent(string file_path)
  29.         {
  30.             string ext = "";
  31.             int dot = file_path.LastIndexOf(".");
  32.             if (dot >= 0)
  33.                 ext = file_path.Substring(dot, file_path.Length - dot).ToUpper();
  34.             if (Contents[ext] == null)
  35.                 return "application/" + ext;
  36.             else
  37.                 return (string)Contents[ext];
  38.         }
  39.         public void WriteHeaderToClient(string content_type, long length)
  40.         {
  41.             string str = "HTTP/1.1 200 OK\nContent-type: " + content_type
  42.                    + "\nContent-Encoding: 8bit\nContent-Length:" + length.ToString()
  43.                    + "\n\n";
  44.             Client.GetStream().Write(Encoding.ASCII.GetBytes(str), 0, str.Length);
  45.         }
  46.         public void WriteToClient(string request)
  47.         {
  48.             string file_path = GetPath(request);
  49.             if (file_path.IndexOf("..") >= 0 || !File.Exists(file_path))
  50.             {
  51.                 WriteHeaderToClient("text/plain", Error_Message.Length);
  52.                 Client.GetStream().Write(Encoding.ASCII.GetBytes(Error_Message), 0, Error_Message.Length);
  53.                 return;
  54.             }
  55.             FileStream file = File.Open(file_path, FileMode.Open);
  56.             WriteHeaderToClient(GetContent(file_path), file.Length);
  57.             byte[] buf = new byte[1024];
  58.             int len;
  59.             while ((len = file.Read(buf, 0, 1024)) != 0)
  60.                 Client.GetStream().Write(buf, 0, len);
  61.             file.Close();
  62.         }
  63.         public void Interact()
  64.         {
  65.             try
  66.             {
  67.                 byte[] buffer = new byte[1024];
  68.                 string request = "";
  69.                 while (true)
  70.                 {
  71.                     int count = Client.GetStream().Read(buffer, 0, 1024);
  72.                     request += Encoding.ASCII.GetString(buffer, 0, count);
  73.                     if (request.IndexOf("\r\n\r\n") >= 0)
  74.                     {
  75.                         WriteToClient(request);
  76.                         request = "";
  77.                     }
  78.                 }
  79.             }
  80.             catch (Exception)
  81.             {
  82.             }
  83.         }
  84.         protected void SetContents()
  85.         {
  86.             Contents.Add("", "application/unknown");
  87.             Contents.Add(".HTML", "text/html");
  88.             Contents.Add(".HTM", "text/html");
  89.             Contents.Add(".TXT", "text/plain");
  90.             Contents.Add(".GIF", "image/gif");
  91.             Contents.Add(".JPG", "image/jpeg");
  92.         }
  93.         public Interaction(TcpClient client)
  94.         {
  95.             Client = client;
  96.             SetContents();
  97.             Thread interact = new Thread(new ThreadStart(Interact));
  98.             interact.Start();
  99.         }
  100.     }
  101.  
  102.     public class Server
  103.     {
  104.         private TcpListener Listener;
  105.  
  106.         public Server(int port)
  107.         {
  108.             Listener = new TcpListener(port);
  109.             Listener.Start();
  110.             Listen();
  111.         }
  112.         ~Server()
  113.         {
  114.             if (Listener != null)
  115.                 Listener.Stop();
  116.         }
  117.         public void Listen()
  118.         {
  119.             while (true)
  120.                 new Interaction(Listener.AcceptTcpClient());
  121.         }
  122.         [STAThread]
  123.         static void Main(string[] args)
  124.         {
  125.             Server server = new Server(8123);
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment