假设我想在 IPC 的某个端口上运行 TCP/IP 服务。当我将端口号传递给我想与之通信的进程时,端口号并不重要。获取系统仍然可用的随机高(通常> 49152)端口号的最佳方法是什么? POSIX 中有什么我可以使用的吗?
我知道 FTP 服务器经常需要这个。
最佳答案
如果您不指定端口号,操作系统将自动选择 ephemeral port给你号码。
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/