gpt4 book ai didi

sockets - 获取一个随机的、仍然可用的高端口号

转载 作者:可可西里 更新时间:2023-11-01 02:30:37 25 4
gpt4 key购买 nike

假设我想在 IPC 的某个端口上运行 TCP/IP 服务。当我将端口号传递给我想与之通信的进程时,端口号并不重要。获取系统仍然可用的随机高(通常> 49152)端口号的最佳方法是什么? POSIX 中有什么我可以使用的吗?

我知道 FTP 服务器经常需要这个。

最佳答案

如果您不指定端口号,操作系统将自动选择 ephemeral port给你号码。

来自 Choosing the Port Number :

New server software should strive to avoid depending on a specific port number, especially if it is user-level software that is not ineluctably tied to a well-known port. Fortunately, this is easily done by requesting port 0, which instructs the system to choose an ephemeral port number.

关于sockets - 获取一个随机的、仍然可用的高端口号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/948122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com