2009-08-18 79 views
1

在此代码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 
] 

为什么首先在

http-port: first wait listen-port 

,而不是仅仅

http-port: wait listen-port 

回答

2

,直到一个新的客户端连接上listen-port块的wait。一旦发生这种情况,它只会返回listen-port。随后的first然后检索对应于新连接的客户端的端口。在此之后,您有两个不同的端口:listen-port(服务器侦听进一步连接的端口)和http-port,这是用于与新连接的客户端交谈的端口。

从REBOL /核心用户指南2.3版的部分"Creating TCP Servers"仍然是非常最新在这些方面。

+0

谢谢,似乎更清楚,我:) – 2009-08-18 22:22:20

0

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

因此,需要first并返回数据从listen-port的第一行。

documentation解释(但是短暂)。

+0

谢谢回答 – 2009-08-18 22:22:53

相关问题