构建windows服务时如何选择使用哪个端口?(Windows 和 .net)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3800111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:37:46  来源:igfitidea点击:

How to choose which port to use when building a windows service? (windows & .net)

windowsweb-serviceshttpport

提问by Rory

I'm writing a windows service which will expose an http RESTful web service for other processes on the machine. This will be deployed to lots of machines on various corporate desktops that I have little/no control over. How should I choose which port my service should listen on?

我正在编写一个 Windows 服务,它将为机器上的其他进程公开一个 http RESTful Web 服务。这将部署到我几乎/无法控制的各种公司桌面上的许多机器上。我应该如何选择我的服务应该侦听的端口?

I'll make it configurable, but need to know how to choose some reasonable default(s).

我将使其可配置,但需要知道如何选择一些合理的默认值。

fyi I'm planning on using .NET 3.5 (unable to use 4.0 for deployment reasons) and WCF with WCF REST Starter Toolkit.

仅供参考,我计划使用 .NET 3.5(由于部署原因无法使用 4.0)和 WCF 和WCF REST Starter Toolkit

UPDATE: to clarify, these are corporate non-development machines. I want to choose a port that's not likely to be used for anything else. I guess from this list of PORT NUMBERS(thanks @Pascal Thivent) that I should choose one in the dynamic/private range

更新:澄清一下,这些是企业非开发机器。我想选择一个不太可能用于其他任何事情的端口。我想从这个端口号列表中(感谢@Pascal Thivent),我应该在动态/私有范围内选择一个

The Dynamic and/or Private Ports are those from 49152 through 65535

动态和/或专用端口是从 49152 到 65535 的端口

So is there any better way of choosing a port within that range, or do I just choose randomly?

那么有没有更好的方法来选择该范围内的端口,还是我只是随机选择?

采纳答案by Rory

Ultimately we chose an arbitrary unused port in the Registered Port range - from 1024 through 49151 - and then made it configurable on the offchance someone comes along and uses that port for something else. Best solution would then be to register that port.

最终,我们在注册端口范围内选择了一个任意未使用的端口 - 从 1024 到 49151 - 然后在有人出现并将该端口用于其他用途时对其进行配置。最好的解决方案是注册该端口。

We chose not to use a dynamic port as processes are able to arbitrarily start using them, so it'd be less consistent whether the port was available.

我们选择不使用动态端口,因为进程可以任意开始使用它们,因此端口是否可用会不太一致。

回答by Pascal Thivent

The officialassignments registred with the Internet Assigned Numbers Authority (IANA) for HTTP are:

官员与互联网编号分配机构(IANA)的HTTP registred分配如下:

  • 80: standard port for HTTP,
  • 8080: HTTP alternate (commonly used by cache or proxy or web server running as a non-root user)
  • 80:HTTP 的标准端口,
  • 8080:HTTP 替代(通常由以非 root 用户身份运行的缓存或代理或 Web 服务器使用)

The ports below are non official ports (not registered with IANA) that are also used:

以下端口是也使用的非官方端口(未在 IANA 注册):

  • 8081: HTTP alternate
  • 8090: HTTP alternate (used as an alternative to port 8080)
  • 8081:HTTP 备用
  • 8090:HTTP 替代(用作端口 8080 的替代)

I don't know what kind of machines you are targeting but if they include development machines, I would probably use 8090 to minimize possible conflicts.

我不知道你的目标是什么类型的机器,但如果它们包括开发机器,我可能会使用 8090 来最大程度地减少可能的冲突。

References

参考

回答by Dan Puzey

If your service is HTTP then you should use port 80 - the standard HTTP port. This is typical of most REST webservices.

如果您的服务是 HTTP,那么您应该使用端口 80 - 标准 HTTP 端口。这是大多数 REST Web 服务的典型特征。