HttpListener port sharing with IIS or another process

Written by Matej Drolc

Here I explore some options of hosting a HttpListener-based process on port 80 alongside another process that binds to port 80. Sharing one tcp port among multiple processes on the same Windows machine can sometimes be possible with some limitations. It mostly depends whether programs are implemented on top of the http.sys driver or not.

About Http.sys implementors

I have a custom web server that uses HttpListener internally. HttpListener (same as IIS) calls the Microsoft HTTP Server API which is an interface for the kernel-mode device driver called HTTP protocol stack (HTTP.sys). Any application that runs on top of http.sys can be made to run alongside and work together with another http.sys app (eg. IIS) on the same listening port. In order to achieve that, urlprefixes must be properly configured.

In my case I needed both IIS and HttpListener to listen on port 80.

As long as urlprefixes do not clash, this is possible.

For example IIS (or another http.sys app/lib) can listen on http://host1:80 and HttpListener on http://host2:80 and it will work.

To show all HTTP.SYS currently registered prefixes, you can use the following command:

netsh http show servicestate | findstr /r "Server\ Session HTTP"

More about UrlPrefixes

Sharing same port with IIS

By default IIS binds to url prefix “http://*:80/“ which is a weak wildcard so all host-specific bindings by HttpListener will take precedence over IIS. So setting HttpListener to listen on “http://app1.mydomain.com:80/“ will work fine. Binding IIS to “http://app2.mydomain.com:80/“ would also work fine.

Segregating HttpListener and a non-http.sys server on different interfaces

Let there be two IPs available (for example 1.1.1.1 and 2.2.2.2) and we want to have HttpListener listen on one and Apache on the other.

  1. Limit HTTP Server API to one IP by doing: netsh http add iplisten 1.1.1.1

    This will limit HTTP Server API and thus HttpListener to only listen on 1.1.1.1.

  2. Make apache bind to 2.2.2.2.

Additional info

Segregating IIS and HttpListener on different interfaces

To bind IIS and HttpListener each to its own IP (for example 1.1.1.1 and 2.2.2.2). Set HttpListener’s urlprefix to “http://1.1.1.1:80“. Set IIS IP binding to “2.2.2.2” which will by default internally translate to a urlprefix of “http://2.2.2.2:80“.