1

在这段代码中

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
]

为什么先入

http-port: first wait listen-port

而不仅仅是

http-port: wait listen-port
4

2 回答 2

2

waitlisten-port客户端连接之前一直阻塞。一旦发生这种情况,它就会简单地返回listen-port。随后first检索与新连接的客户端对应的端口。在此之后,您有两个不同的端口:listen-port,这是服务器侦听进一步连接http-port的端口,以及 ,这是与新连接的客户端通信的端口。

REBOL/Core 用户指南 2.3 版中的“创建 TCP 服务器”部分在这些方面仍然是最新的。

于 2009-08-18T20:17:54.863 回答
0

wait阻塞直到监听端口有活动,然后返回listen-port

因此,first从 获取并返回第一行数据listen-port

文档解释(但简短)。

于 2009-08-18T00:43:43.157 回答