Table of Contents

Class HttpServer

Namespace
NanoByte.Common.Net
Assembly
NanoByte.Common.dll

A simple HTTP server.

[CLSCompliant(false)]
[MustDisposeResource]
public abstract class HttpServer : IDisposable
Inheritance
object
HttpServer
Implements
Derived
Extension Methods

Constructors

HttpServer(ushort, bool)

Gets ready to serve HTTP requests. Call StartHandlingRequests() after completing any additional setup.

protected HttpServer(ushort port = 0, bool localOnly = false)

Parameters

port ushort

The TCP port to listen on; 0 to automatically pick free port.

localOnly bool

true to only respond to requests from the local machine instead of the network.

Exceptions

WebException

Unable to serve on the specified port, or unable to find a free port if port is 0.

NotAdminException

Needs admin rights to serve HTTP requests.

Properties

IsListening

Indicates whether the server is currently accepting requests.

public bool IsListening { get; }

Property Value

bool

Remarks

Becomes false after Dispose() or if the server stopped due to an unrecoverable failure.

MaxConcurrentRequests

The maximum number of requests to handle at the same time. Additional requests are rejected with ServiceUnavailable.

public int MaxConcurrentRequests { get; set; }

Property Value

int

Port

The TCP port the server is listening on.

public ushort Port { get; }

Property Value

ushort

Methods

Dispose()

Stops serving HTTP requests. Does not wait for requests that are currently being handled to complete.

public virtual void Dispose()

HandleRequest(HttpListenerContext)

Handles a single HTTP request.

protected abstract void HandleRequest(HttpListenerContext context)

Parameters

context HttpListenerContext

StartHandlingRequests()

To be called by derived constructor when setup is complete.

protected void StartHandlingRequests()