简体   繁体   English

我应该使用什么端口号?

[英]What port number shall I use?

I am writing an app that is to be running on a Windows PC.我正在编写一个要在 Windows PC 上运行的应用程序。 I need to create a server socket listening on 127.0.0.1, and another client socket which is to connect with this server socket.我需要创建一个侦听 127.0.0.1 的服务器套接字,以及另一个与该服务器套接字连接的客户端套接字。

Since the data exchange between the two sockets are within the same machine and there is no client connecting from outside of the machine, what port to use is insignificant, as long as the two sockets use the same port number.由于两台sockets之间的数据交换是在同一台机器内,没有从机外连接的客户端,使用什么端口无关紧要,只要两台sockets使用相同的端口号即可。

So, how do I decide which port number to use ?那么,我如何决定使用哪个端口号 Shall it be a hard-coded port number such as 49500?应该是硬编码的端口号,比如 49500? What if another unrelated app on this machine happen to use this port number?如果这台机器上的另一个不相关的应用程序碰巧使用了这个端口号怎么办? Or shall I get the list of all used ports and programmatically pick an unused port?或者我应该得到所有使用端口的列表并以编程方式选择一个未使用的端口?

Just want to know what is the best approach.只想知道什么是最好的方法。 Thanks.谢谢。

ports within 0 to 1023 are generally controlled and you should assign your socket with higher port numbers, although in that range ports within 1024 and 49151 can be registered for others to be informed about that and not use them. 0 到 1023 之间的端口通常是受控制的,您应该为您的套接字分配更高的端口号,尽管在该范围内可以注册 1024 和 49151 内的端口,以便其他人了解这一点而不使用它们。
if you want to avoid conflicts you can see registered ports on your machine and assign a port number to your socket which is empty but ports higher than that (49152 to 65535) are completely free and are not even registered.如果您想避免冲突,您可以在您的机器上查看已注册的端口,并为您的套接字分配一个端口号,该端口号为空,但高于该端口(49152 到 65535)的端口是完全免费的,甚至没有注册。
generally, it is not common to worry about that.一般来说,担心这一点并不常见。 for example, two major applications like VMware and apache web server operate on the same port number (443), and if you want to use VMware workstation and Xampp (which works with apache) you have to simply make one of them listen on another port and its not a big deal.例如,VMware 和 apache web 服务器等两个主要应用程序在相同的端口号 (443) 上运行,如果您想使用 VMware 工作站和 Z3D7A0855B865B4332A94F903EF782A94F94F903EF78224DZ(它们在另一个端口上侦听),您只需制作一个 apache)这没什么大不了的。 so in my opinion the best practice is to let your users change this via a config file or something similar.所以在我看来,最好的做法是让你的用户通过配置文件或类似的东西来改变它。
for further information, you can search google.更多信息,你可以搜索谷歌。 for instance this link might be useful:例如这个链接可能有用:
https://www.sciencedirect.com/topics/computer-science/registered-port#:~:text=Well%2Dknown%20ports%E2%80%94Ports%20in,1023%20are%20assigned%20and%20controlled.&text=Registered%20ports%E2%80%94Ports%20in%20the,be%20registered%20to%20prevent%20duplication.&text=Dynamic%20ports%E2%80%94Ports%20in%20the,assigned%2C%20controlled%2C%20or%20registered . https://www.sciencedirect.com/topics/computer-science/registered-port#:~:text=Well%2Dknown%20ports%E2%80%94Ports%20in,1023%20are%20assigned%20and%20controlled.&text =已注册%20ports%E2%80%94Ports%20in%20the,be%20registered%20to%20prevent%20duplication.&text=动态%20ports%E2%80%94Ports%20in%20the,assigned%2C%20受控%2C%20或%20已注册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Tcpsocket 中使用箭头 function? - How can i use arrow function in Tcpsocket? Ruby TCPSocket服务器-我可以告诉客户端连接的主机吗? - Ruby TCPSocket Server - Can I tell to what host a client was connecting? 如何在组件注释的 bean 中使用会话或请求范围的 bean? - How can I use a session or request-scoped bean in a component annotated bean? 连接到本地主机的端口 9999 时连接被拒绝 - Connection refused when connecting to localhost's port 9999 使用动态主机和端口的 TCP 套接字客户端的 Spring 集成 - Spring Integration for TCP Socket Client using Dynamic Host and Port 在给定超时后从 Ruby 的 TCPSocket::gets 逃脱的最佳方法是什么? - What's the best way to escape from Ruby's TCPSocket::gets after a given timeout? 当我们想使用Javascript通过WebSocket进行通信时,套接字如何将端口从HTTP 80更改为另一个端口? - How does a socket change ports from HTTP 80 to another port when we want to communicate over WebSocket using Javascript? 确认:我可以在保持 TCP 套接字打开的同时调试应用程序吗? - Confirmation: can I debug an application while keeping a TCP socket open? 如何使用多线程处理 50 个客户端到服务器 - How do I handle 50 clients to server using multithreading 为什么我不能在没有 \\n 的情况下运行这个 tcp 客户端套接字代码? - Why can't I run this tcp client socket code without \n?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM