How can I create an HttpListener class on a random port in C#?

后端 未结 7 1457
广开言路
广开言路 2020-12-03 16:34

I would like to create an application that serves web pages internally and can be run in multiple instances on the same machine. To do so, I would like to create an H

7条回答
  •  自闭症患者
    2020-12-03 17:29

    Unfortunately, this isn't possible. As Richard Dingwall already suggested, you could create a TCP listener and use that port. This approach has two possible problems:

    • In theory a race condition may occur, because another TCP listener might use this port after closing it.
    • If you're not running as an Administrator, then you need to allocate prefixes and port combinations to allow binding to it. This is impossible to manage if you don't have administrator privileges. Essentially this would impose that you need to run your HTTP server with administrator privileges (which is generally a bad idea).

提交回复
热议问题