Rebol 世界上最小的 Http 服务器:为什么要先等待监听端口?
在这段代码中
web-dir: %./www/ ; the path to rebol www subdirectory
listen-port: open/lines tcp://:80 ; port used for web connections
buffer: make string! 1024 ; will auto-expand if needed
forever [
http-port: first wait listen-port
while [not empty? client-request: first http-port][
repend buffer [client-request newline]
]
repend buffer ["Address: " http-port/host newline]
parse buffer ["get" ["http" | "/ " | copy file to " "]]
parse file [thru "." [
"html" (mime: "text/html") |
"txt" (mime: "text/plain")
]
]
data: read/binary web-dir/:file
insert data rejoin ["HTTP/1.0 200 OK^/Content-type: " mime "^/^/"]
write-io http-port data length? data
close http-port
]
为什么是first in
http-port: first wait listen-port
而不是just
http-port: wait listen-port
In this code
web-dir: %./www/ ; the path to rebol www subdirectory
listen-port: open/lines tcp://:80 ; port used for web connections
buffer: make string! 1024 ; will auto-expand if needed
forever [
http-port: first wait listen-port
while [not empty? client-request: first http-port][
repend buffer [client-request newline]
]
repend buffer ["Address: " http-port/host newline]
parse buffer ["get" ["http" | "/ " | copy file to " "]]
parse file [thru "." [
"html" (mime: "text/html") |
"txt" (mime: "text/plain")
]
]
data: read/binary web-dir/:file
insert data rejoin ["HTTP/1.0 200 OK^/Content-type: " mime "^/^/"]
write-io http-port data length? data
close http-port
]
Why first in
http-port: first wait listen-port
instead of just
http-port: wait listen-port
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
listen-port
上的wait
会阻塞,直到有新客户端连接为止。 一旦发生这种情况,它只会返回listen-port
。 随后的first
然后检索与新连接的客户端对应的端口。 之后,您有两个不同的端口:listen-port
,这是服务器侦听进一步连接的端口,以及http-port
,这是用于通话的端口到新连接的客户端。REBOL 中的“创建 TCP 服务器” 部分/Core Users Guide 2.3 版在这些方面仍然是最新的。
The
wait
on thelisten-port
blocks until a new client connects. Once that happens, it simply returnslisten-port
. The subsequentfirst
then retrieves port corresponding to the newly connected client. After this, you have two distinct ports:listen-port
, which is the port the server listens on for further connects, andhttp-port
, which is the port for talking to the newly connected client.The section "Creating TCP Servers" from the REBOL/Core Users Guide for version 2.3 is still perfectly up to date in those regards.
wait
阻塞,直到监听端口有活动,然后返回listen-port
。因此,
first
获取并返回来自listen-port
的第一行数据。文档 进行了解释(但很简短)。
wait
blocks until listen-port has activity, then returnslisten-port
.Thus,
first
takes and returns the first line of data fromlisten-port
.The documentation explains (however briefly).