2010-09-10 62 views
0

我試圖通過在紅寶石的套接字來讀取HTTP報文,出於某種原因,我的代碼保持阻斷,同時調用接收讀取數據

socket = TCPSocket.new("localhost",80) 
socket.send r.join("\r\n"), 0 
socket.flush 
#We're only interested in the string "HTTP/1.1 XXX" 
if http_status = get_http_status(socket.recv_nonblock(12)) 
    if http_status == 200 
     $logger.info "Wrote #{message_packet}" 
    else 
     $logger.warn "Resubmitting message because #{line} is not 200" 
     @queue.publish(message) 
    end 
end 

#Get rid of the rest of the content 
the_rest = socket.recv(1000) 
while(the_rest != "") do 
    the_rest = socket.recv(1000)  
end 

從我瞭解recv如果沒有數據在套接字上等待,它應該什麼都不返回?

+0

這個問題寫得非常糟糕,缺少一些非常重要的數據,這些數據實際上使這個問題變得有意義。代碼也是錯誤的。請關閉它。 – Ceilingfish 2010-09-16 09:36:03

回答

0

連接可以是保持連接。所以,你的recv()調用不會自行結束。您需要閱讀Content-Lenght標頭,然後從內容中讀取很多字節。之後,您可以離開RECV循環。

或者,通過設置「Connection:close」標題發出請求,以便服務器在發送響應後關閉連接。