Click here to Skip to main content
14,297,259 members

TinyWeb: A Simple Web Server in C#

Rate this:
4.95 (18 votes)
Please Sign up or sign in to vote.
4.95 (18 votes)
11 Sep 2019CPOL
A simple home HTTP server that works across all .NET platforms

 

Note: This project only serves single requests at once. Try this one instead. It's better: Tiny Web Server Take 2

Introduction

Currently, the offerings for .NET enabled web servers are platform dependent, and large, intended for scalable sites. Often, they have reams of features and lots of configuration. The simplest offering, http.sys, only works on windows.

The purpose of this web server is to expose a small, limited scalability server for something like a home network, or another scenario where the connection frequency is relatively low, and users are trusted.

It works on any .NET platform, and its component based API is simple to operate. Just drag it onto a form or a service component, wire up the events, and set the properties. Aside from the local endpoint to listen on, there is zero configuration.

Using the Code

Using the code is fairly simple:

using TinyWeb;
using System.Net;
...
var webServer = new WebServer(); 
webServer.ProcessRequest += new ProcessRequestEventHandler(webServer_ProcessRequest);
webServer.EndPoint = new IPEndPoint(IPAddress.Any,8080);
webServer.IsStarted = true;
...
void webServer_ProcessRequest(object sender, ProcessRequestEventArgs args)
{
    var r = args.Response;
    r.ContentType = "text/html";
    r.WriteLine("<html><h1>Hello World!</h1></html>");
}

Request has methods for getting the headers, the querystring, and the request/post stream. Response has methods for setting the response headers, and for writing the response stream.

Points of Interest

SocketUtility contains many other methods for working with sockets, especially asynchronous socket communication, including exposing awaitable methods for the primary socket operations.

History

  • 28th August, 2019 - Initial submission

License

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

Share

About the Author

honey the codewitch
United States United States
Just a shiny lil monster. Casts spells in C++. Mostly harmless.

Comments and Discussions

 
BugNew code at the Take 2 link - this one shouldn't really be used Pin
honey the codewitch11-Sep-19 4:18
memberhoney the codewitch11-Sep-19 4:18 
QuestionI like the idea but multiple clients cannot access it at the same time Pin
RudolfHenning5-Sep-19 20:24
memberRudolfHenning5-Sep-19 20:24 
AnswerRe: I like the idea but multiple clients cannot access it at the same time Pin
Matt Slay10-Sep-19 0:38
memberMatt Slay10-Sep-19 0:38 
GeneralRe: I like the idea but multiple clients cannot access it at the same time Pin
honey the codewitch10-Sep-19 22:54
memberhoney the codewitch10-Sep-19 22:54 
GeneralRe: I like the idea but multiple clients cannot access it at the same time Pin
Matt Slay11-Sep-19 4:15
memberMatt Slay11-Sep-19 4:15 
GeneralRe: I like the idea but multiple clients cannot access it at the same time Pin
honey the codewitch11-Sep-19 4:17
memberhoney the codewitch11-Sep-19 4:17 
GeneralRe: I like the idea but multiple clients cannot access it at the same time Pin
RudolfHenning12-Sep-19 1:15
memberRudolfHenning12-Sep-19 1:15 
AnswerRe: I like the idea but multiple clients cannot access it at the same time Pin
honey the codewitch10-Sep-19 22:54
memberhoney the codewitch10-Sep-19 22:54 
GeneralA readme file only? Pin
Gustav Brock3-Sep-19 2:57
professionalGustav Brock3-Sep-19 2:57 
GeneralRe: A readme file only? Pin
honey the codewitch10-Sep-19 22:53
memberhoney the codewitch10-Sep-19 22:53 
PraiseKudos for posting your code for the world to inspect! Pin
User0`01-Sep-19 13:41
memberUser0`01-Sep-19 13:41 
GeneralRe: Kudos for posting your code for the world to inspect! Pin
honey the codewitch2-Sep-19 4:47
memberhoney the codewitch2-Sep-19 4:47 
QuestionThis article contains very little content Pin
Grober_31-Aug-19 5:50
memberGrober_31-Aug-19 5:50 
Please improve the article
AnswerRe: This article contains very little content Pin
CHill6011-Sep-19 5:41
protectorCHill6011-Sep-19 5:41 
QuestionServer does not process request from more than one browser?? Pin
Matt Slay31-Aug-19 1:31
memberMatt Slay31-Aug-19 1:31 
AnswerRe: Server does not process request from more than one browser?? Pin
honey the codewitch31-Aug-19 4:25
memberhoney the codewitch31-Aug-19 4:25 
QuestionwebServer_ProcessRequest() method gets called *TWICE* for each request... Pin
Matt Slay30-Aug-19 15:41
memberMatt Slay30-Aug-19 15:41 
AnswerRe: webServer_ProcessRequest() method gets called *TWICE* for each request... Pin
honey the codewitch30-Aug-19 20:49
memberhoney the codewitch30-Aug-19 20:49 
GeneralRe: webServer_ProcessRequest() method gets called *TWICE* for each request... Pin
Matt Slay31-Aug-19 1:01
memberMatt Slay31-Aug-19 1:01 
GeneralRe: webServer_ProcessRequest() method gets called *TWICE* for each request... Pin
honey the codewitch31-Aug-19 4:24
memberhoney the codewitch31-Aug-19 4:24 
PraiseNice! Pin
qmartens30-Aug-19 13:11
memberqmartens30-Aug-19 13:11 
QuestionAnother option besides "rolling your own" Pin
gardner903230-Aug-19 11:35
membergardner903230-Aug-19 11:35 
AnswerRe: Another option besides "rolling your own" Pin
Matt Slay30-Aug-19 15:44
memberMatt Slay30-Aug-19 15:44 
AnswerRe: Another option besides "rolling your own" Pin
honey the codewitch30-Aug-19 20:50
memberhoney the codewitch30-Aug-19 20:50 
GeneralRe: Another option besides "rolling your own" Pin
gardner903231-Aug-19 6:19
membergardner903231-Aug-19 6:19 

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.

Tip/Trick
Posted 29 Aug 2019

Stats

11.8K views
750 downloads
36 bookmarked