Click here to Skip to main content
15,867,568 members
Articles / General Programming / Internet
Tip/Trick

Create a local server in C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
28 Oct 2012CPOL1 min read 94.1K   3.5K   16   8
A tip about how to create a local server in C#.

Image 1
Introduction

In this tip, I'll tell you how to create a local server in C#.

Using the code

At the top of your code file, add this using namespace statements:
C#
using System.Net;
using System.Threading;

In the Program class, create a new instance of a HttpListener:

static HttpListener _httpListener = new HttpListener();

Then, create the Main(string[] args) method:

static void Main(string[] args)
{
      Console.WriteLine("Starting server...");
      _httpListener.Prefixes.Add("http://localhost:5000/"); // add prefix "http://localhost:5000/"
      _httpListener.Start(); // start server (Run application as Administrator!)
      Console.WriteLine("Server started.");
      Thread _responseThread = new Thread(ResponseThread);
      _responseThread.Start(); // start the response thread
 }

If you add a prefix, you need to terminate the prefix with a forward slash ("/"). You can add more then one prefix.

Before we can run the application, we need to create the ResponseThread method. And then, we must run the application as administrator.

The ResponseThread method:

static void ResponseThread()
{
     while (true)
     {
           HttpListenerContext context = _httpListener.GetContext(); // get a context
           // Now, you'll find the request URL in context.Request.Url
           byte[] _responseArray = Encoding.UTF8.GetBytes("<html><head><title>Localhost server -- port 5000</title></head>" + 
           "<body>Welcome to the <strong>Localhost server</strong> -- <em>port 5000!</em></body></html>"); // get the bytes to response
           context.Response.OutputStream.Write(_responseArray, 0, _responseArray.Length); // write bytes to the output stream
           context.Response.KeepAlive = false; // set the KeepAlive bool to false
           context.Response.Close(); // close the connection
           Console.WriteLine("Respone given to a request.");
     }
}

First, you need to get a request (in the context). If you've a context, you'll find the request in context.Request. And context.Request.Url is the request URL. Then, you can write a response to context.Response.OutputStream. With Encoding.UTF8.GetBytes you can get the bytes of a string. The response must be HTML.

The name of the server

The name of the server can be:

  1. http://localhost/ (eventually with a port, such as http://localhost:5000/)
  2. http://[your local IP address]/ (eventually with a port, such as http://[your local IP address]:5000/)
  3. http://[IP from 127.0.0.1 to 127.255.255.254]/ (eventually with a port, such as http://127.0.0.1:5000/)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Europe Europe
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow can I share html file by this local server? Pin
Ibrahim Fares31-Jan-20 6:11
Ibrahim Fares31-Jan-20 6:11 
AnswerRe: how can I share html file by this local server? Pin
Thomas Daniels31-Jan-20 6:13
mentorThomas Daniels31-Jan-20 6:13 
GeneralRe: how can I share html file by this local server? Pin
Ibrahim Fares31-Jan-20 7:50
Ibrahim Fares31-Jan-20 7:50 
GeneralRe: how can I share html file by this local server? Pin
Thomas Daniels31-Jan-20 9:25
mentorThomas Daniels31-Jan-20 9:25 
QuestionHow can i run this url via mobile phone Pin
Member 1297727710-Sep-19 23:18
Member 1297727710-Sep-19 23:18 
AnswerRe: How can i run this url via mobile phone Pin
Thomas Daniels10-Sep-19 23:21
mentorThomas Daniels10-Sep-19 23:21 
You have to find the local IP of your laptop (if you're on Windows use ipconfig, if you're on Linux use ifconfig), and then the URL is http://[local IP of laptop]:5000/
GeneralMy vote of 5 Pin
CS14019-Apr-13 2:04
CS14019-Apr-13 2:04 
GeneralRe: My vote of 5 Pin
Thomas Daniels9-Apr-13 5:15
mentorThomas Daniels9-Apr-13 5:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.