[英]Reading data from a socket in ruby in a multithreaded environment
我試圖通過ruby中的套接字讀取HTTP數據包,由於某種原因,我的代碼在調用recv時一直處於阻塞狀態
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
了解,如果套接字上沒有數據等待,它應該什么也不返回?
該連接可以是保持活動連接。 因此,您的recv()調用不會自行結束。 您將需要讀取Content-Lenght標頭,然后從內容中讀取那么多字節。 之后,您可以退出RECV循環。
或者,通過設置“ Connection:close”標頭來發出請求,以便服務器在發送響應后關閉連接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.