未指定时,Racket 的 servlet 如何决定 运行 的端口?
How do Racket's servlets decide what port to run on when unspecified?
以下代码片段打开一个 servlet,但每次都在不同的端口上。
#lang web-server/insta
(define (start req)
(response/xexpr
`(html (head (title "Hello world!"))
(body (p "Hey out there!")))))
documentation explains how to specify a port number, if desired. But I am curious how web-server/insta
decides what port to run on when unspecified. I found the source on Github here 但我的球拍远未达到标准,我无法理解我在那里阅读的内容。有谁知道端口是如何选择的吗?
谢谢!
编辑:
刚了解“临时端口”。
临时端口是操作系统在程序请求任何可用的用户端口时创建的短期端点。操作系统从预定义的范围内选择端口号,通常在 1024 到 65535 之间,并在相关的 TCP 连接终止后释放该端口。 - VMware 文档
所以 Racket 似乎很可能只是在请求任何可用的端口,并且它是 操作系统 返回一个“临时端口”。尽管如此,如果有人能指出发生这种情况的 Racket 网络服务器中的实际源代码,我想证实这一理论。
源代码中的相关行是here,以下是摘录:
(serve/servlet (contract (request? . -> . can-be-response?) #,start
'you 'web-server
"start"
#f)
<b>#:port 0</b>
#:extra-files-paths (if extra-files-path (list extra-files-path) empty)
#:launch-browser? launch-browser?)</pre>
注意 #:port 0
行。虽然我没有追溯这个端口被传递到哪里,但我假设它最终从 racket/tcp
提供给 Racket 的 tcp-listen
函数,其中包括以下行 in its documentation:
If port-no
is 0
the socket binds to an ephemeral port, which can be determined by calling tcp-addresses
.
我的猜测是 OS 确实执行了此分配,但我没有深入挖掘。
以下代码片段打开一个 servlet,但每次都在不同的端口上。
#lang web-server/insta
(define (start req)
(response/xexpr
`(html (head (title "Hello world!"))
(body (p "Hey out there!")))))
documentation explains how to specify a port number, if desired. But I am curious how web-server/insta
decides what port to run on when unspecified. I found the source on Github here 但我的球拍远未达到标准,我无法理解我在那里阅读的内容。有谁知道端口是如何选择的吗?
谢谢!
编辑: 刚了解“临时端口”。
临时端口是操作系统在程序请求任何可用的用户端口时创建的短期端点。操作系统从预定义的范围内选择端口号,通常在 1024 到 65535 之间,并在相关的 TCP 连接终止后释放该端口。 - VMware 文档
所以 Racket 似乎很可能只是在请求任何可用的端口,并且它是 操作系统 返回一个“临时端口”。尽管如此,如果有人能指出发生这种情况的 Racket 网络服务器中的实际源代码,我想证实这一理论。
源代码中的相关行是here,以下是摘录:
(serve/servlet (contract (request? . -> . can-be-response?) #,start 'you 'web-server "start" #f) <b>#:port 0</b> #:extra-files-paths (if extra-files-path (list extra-files-path) empty) #:launch-browser? launch-browser?)</pre>
注意
#:port 0
行。虽然我没有追溯这个端口被传递到哪里,但我假设它最终从racket/tcp
提供给 Racket 的tcp-listen
函数,其中包括以下行 in its documentation:If
port-no
is0
the socket binds to an ephemeral port, which can be determined by callingtcp-addresses
.我的猜测是 OS 确实执行了此分配,但我没有深入挖掘。