HttpListener: как слушать запросы локальной сети? - C#

Узнай цену своей работы

Формулировка задачи:

Почему у меня получается зайти на сервер только на ПК на котором запущенна эта программа, мне сказали, что чтобы могли подключаться из локальной сету нужно запустить в несколько потоков, но не выходит. Так как получить доступ к серверу из локальной сети???
Листинг программы
  1. class Local_Host
  2. {
  3. public HttpListener listener1;
  4. public string html;
  5. public void StartHost(string ip, int port, string prefix, int count)
  6. {
  7. listener1 = new HttpListener();
  8. listener1.Prefixes.Add("http://" + ip + ":" + port + "/" + prefix + "/");
  9. listener1.Start();
  10.  
  11. for (int i = 0; i <= count; i++)
  12. ThreadPool.QueueUserWorkItem(Potock);
  13. }
  14. async void Potock(object state)
  15. {
  16. while (true)
  17. {
  18. HttpListenerContext context = await listener1.GetContextAsync();
  19. HttpListenerRequest request = context.Request;
  20. HttpListenerResponse response = context.Response;
  21. string responseString = System.IO.File.ReadAllText(html).Replace("\n", " ");
  22. byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
  23. response.ContentLength64 = buffer.Length;
  24. Stream output = response.OutputStream;
  25. output.Write(buffer, 0, buffer.Length);
  26. output.Close();
  27. }
  28. }
  29. }

Решение задачи: «HttpListener: как слушать запросы локальной сети?»

textual
Листинг программы
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Text;
  5. using System.Threading;
  6.  
  7. namespace ConsoleApplication209
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var server = new Local_Host(){html = "c:\\1.html"};
  14.             server.StartHost("192.168.0.104", 22234, "pref");
  15.  
  16.             Console.ReadLine();
  17.         }
  18.  
  19.         class Local_Host
  20.         {
  21.             public HttpListener listener1;
  22.  
  23.             public string html;
  24.             public void StartHost(string ip, int port, string prefix)
  25.             {
  26.  
  27.                 listener1 = new HttpListener();
  28.                 listener1.Prefixes.Add("http://" + ip + ":" + port + "/" + prefix + "/");
  29.                 listener1.Start();
  30.  
  31.  
  32.                 ThreadPool.QueueUserWorkItem(Potock);
  33.             }
  34.  
  35.             void Potock(object state)
  36.             {
  37.                 while (true)
  38.                 {
  39.                     HttpListenerContext context = listener1.GetContext();
  40.                     HttpListenerRequest request = context.Request;
  41.                     HttpListenerResponse response = context.Response;
  42.  
  43.                     string responseString = File.ReadAllText(html).Replace("\n", " ");
  44.  
  45.                     byte[] buffer = Encoding.UTF8.GetBytes(responseString);
  46.                     response.ContentLength64 = buffer.Length;
  47.                     Stream output = response.OutputStream;
  48.                     output.Write(buffer, 0, buffer.Length);
  49.                     output.Close();
  50.                 }
  51.             }
  52.         }
  53.     }
  54. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

14   голосов , оценка 4.429 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы