Echo server in c#

A friend of mine asked me some help to troubleshoot accessing a device in his home network from internet.

I'm not an expert of networks but with the help of www.portforward.com and a simple c# winform I succesfully manage to made things work.

The program listen on the specified port and responds with a simple hello message and the ip of the caller.

This is handy if you don't know what the service should reply and you want to be sure that the problem didn't lie on the service not replying correctly.

The program uses HttpListener that leverage the http.sys stack. Beware that with windows Vista you have to run it with administrator privilege to listen to the specified port.

 

   1: using System;
   2: using System.ComponentModel;
   3: using System.Windows.Forms;
   4: using System.Net;
   5: using System.Diagnostics;
   6: using System.Configuration;
   7: using System.Collections.Specialized;
   8:  
   9: namespace EchoServer
  10: {
  11:     public partial class Form1 : Form
  12:     {
  13:         const int maxThread = 1;
  14:         const int listeningPort = 12346;
  15:         HttpListener proxyListener = null;
  16:  
  17:         public Form1()
  18:         {
  19:             InitializeComponent();
  20:             
  21:             NameValueCollection appSettings = ConfigurationManager.AppSettings;
  22:  
  23:  
  24:             txtPortNumber.Text = appSettings.Get("port"); 
  25:         }
  26:  
  27:         private void btnStartStop_Click(object sender, EventArgs e)
  28:         {
  29:             proxyListener = new HttpListener();
  30:             proxyListener.Prefixes.Add("http://+:" + txtPortNumber.Text + "/");
  31:             proxyListener.Start();
  32:  
  33:             int count = 0;
  34:             BackgroundWorker[] listeners = new BackgroundWorker[maxThread];
  35:             this.Text = "listening on port " + listeningPort ;
  36:  
  37:             TraceMessage("Creating background worker threads on main thread " + System.Threading.Thread.CurrentThread.ManagedThreadId);
  38:  
  39:             do
  40:             {
  41:                 listeners[count] = new BackgroundWorker();
  42:                 listeners[count].WorkerReportsProgress = true;
  43:  
  44:                 listeners[count].DoWork += StartListening;
  45:                 listeners[count].RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
  46:                 listeners[count].RunWorkerAsync();
  47:  
  48:             } while (count++ < maxThread - 1);
  49:             TraceMessage("Background threads initialization ended. Reading configuration from " + System.Windows.Forms.Application.LocalUserAppDataPath);
  50:  
  51:  
  52:         }
  53:  
  54:         public void StartListening(object sender, DoWorkEventArgs e)
  55:         {
  56:             BackgroundWorker worker = sender as BackgroundWorker;
  57:  
  58:             try
  59:             {
  60:                 object[] valori = new object[1];
  61:                 HttpListenerContext myContext = proxyListener.GetContext();
  62:                 HttpListenerRequest myRequest = myContext.Request;
  63:                 HttpListenerResponse myResponse = myContext.Response;
  64:                 byte[] responseText = System.Text.ASCIIEncoding.ASCII.GetBytes("Hello " + myRequest.RemoteEndPoint.Address + ":" + myRequest.RemoteEndPoint.Port);
  65:                 myResponse.OutputStream.Write(responseText, 0, responseText.Length);
  66:                 myResponse.Close();
  67:             }
  68:             catch (Exception exc)
  69:             {
  70:                 System.Diagnostics.Trace.WriteLine("Error::" + exc);
  71:             }
  72:         }
  73:         private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  74:         {
  75:             TraceMessage("backgroundWorker1_RunWorkerCompleted start ");
  76:             ((BackgroundWorker)sender).RunWorkerAsync();
  77:             TraceMessage("backgroundWorker1_RunWorkerCompleted end");
  78:         }
  79:  
  80:         public void TraceMessage(string message)
  81:         {
  82:             StackFrame CallStack = new StackFrame(1, true);
  83:             System.Diagnostics.Trace.WriteLine(CallStack.GetFileName().Substring(CallStack.GetFileName().LastIndexOf("\\") + 1) + " [" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString("00") + "] " + message + " " + CallStack.GetMethod().Name + "(" + CallStack.GetFileLineNumber().ToString() + ")");
  84:         }
  85:     }
  86: }

Hope this helps someone.

«gennaio»
domlunmarmergiovensab
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789