Created
November 1, 2012 16:06
-
-
Save dynajoe/3994603 to your computer and use it in GitHub Desktop.
Example C# HTTP Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using System.Text; | |
using System.Threading; | |
namespace ExampleSimpleWebserver | |
{ | |
class Program | |
{ | |
static void Main (string[] args) | |
{ | |
if (args.Length >= 1 && args[0] == "async") | |
{ | |
Console.WriteLine("Running async server."); | |
new AsyncServer(); | |
} | |
else | |
{ | |
Console.WriteLine("Running sync server."); | |
new SyncServer(); | |
} | |
} | |
} | |
public class SyncServer | |
{ | |
public SyncServer() | |
{ | |
var listener = new HttpListener(); | |
listener.Prefixes.Add("http://localhost:8081/"); | |
listener.Prefixes.Add("http://127.0.0.1:8081/"); | |
listener.Start(); | |
while (true) | |
{ | |
try | |
{ | |
var context = listener.GetContext(); //Block until a connection comes in | |
context.Response.StatusCode = 200; | |
context.Response.SendChunked = true; | |
int totalTime = 0; | |
while (true) | |
{ | |
if (totalTime % 3000 == 0) | |
{ | |
var bytes = Encoding.UTF8.GetBytes(new string('3', 1000) + "\n"); | |
context.Response.OutputStream.Write(bytes, 0, bytes.Length); | |
} | |
if (totalTime % 5000 == 0) | |
{ | |
var bytes = Encoding.UTF8.GetBytes(new string('5', 1000) + "\n"); | |
context.Response.OutputStream.Write(bytes, 0, bytes.Length); | |
} | |
Thread.Sleep(1000); | |
totalTime += 1000; | |
} | |
} | |
catch (Exception) | |
{ | |
// Client disconnected or some other error - ignored for this example | |
} | |
} | |
} | |
} | |
public class AsyncServer | |
{ | |
public AsyncServer() | |
{ | |
var listener = new HttpListener(); | |
listener.Prefixes.Add("http://localhost:8081/"); | |
listener.Prefixes.Add("http://127.0.0.1:8081/"); | |
listener.Start(); | |
while (true) | |
{ | |
try | |
{ | |
var context = listener.GetContext(); | |
ThreadPool.QueueUserWorkItem(o => HandleRequest(context)); | |
} | |
catch (Exception) | |
{ | |
// Ignored for this example | |
} | |
} | |
} | |
private void HandleRequest(object state) | |
{ | |
try | |
{ | |
var context = (HttpListenerContext)state; | |
context.Response.StatusCode = 200; | |
context.Response.SendChunked = true; | |
int totalTime = 0; | |
while (true) | |
{ | |
if (totalTime % 3000 == 0) | |
{ | |
var bytes = Encoding.UTF8.GetBytes(new string('3', 1000) + "\n"); | |
context.Response.OutputStream.Write(bytes, 0, bytes.Length); | |
} | |
if (totalTime % 5000 == 0) | |
{ | |
var bytes = Encoding.UTF8.GetBytes(new string('5', 1000) + "\n"); | |
context.Response.OutputStream.Write(bytes, 0, bytes.Length); | |
} | |
Thread.Sleep(1000); | |
totalTime += 1000; | |
} | |
} | |
catch (Exception) | |
{ | |
// Client disconnected or some other error - ignored for this example | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var helperArray = new Array(1000); | |
var server = http.createServer(function (req, res) { | |
res.writeHead(200); | |
res.on('close', function () { | |
clearInterval(threes); | |
clearInterval(fives); | |
}); | |
var threes = setInterval(function () { | |
res.write(helperArray.join("3") + "\n"); | |
}, 3000); | |
var fives = setInterval(function () { | |
res.write(helperArray.join("5") + "\n"); | |
}, 5000); | |
}); | |
server.listen(8082); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment