From: 7stud -- Date: 2007-10-30T18:09:22+09:00 Subject: Re: Problem with socket.recv() Robert Klemme wrote: > 2007/10/30, Francis Cianfrocca : >> >> If the server closes the connection after sending the data, you can simply >> use socket#read. If you need to wait for the data and then process them as >> they comes in, use #select in a loop until all the data have been received. > > There is another option - even if the server does not close after > sending: use #read with a given size limit. #read will block and > return as soon data is available. That's not what I'm seeing. What I see is: read() blocks until it either receives the limit number of bytes or eof is encountered(when the server closes the socket). In other words, read() does not return as soon as data is available if the amount of bytes read is less than the limit number of bytes. On the other hand, recv() returns whatever it reads immediately. Here is the code: #server: -------------- require 'socket' port = 3030 server = TCPServer.new('', port) while(conn = server.accept) #serves forever conn.print 'h' sleep(5) conn.print 'ello world' conn.close end -------------- #client using recv(): -------------------- require 'socket' port = 3030 socket = TCPSocket.new('localhost', port) all_data = [] while true partial_data = socket.recv(1012) puts partial_data if partial_data.length == 0 break end all_data << partial_data end socket.close puts all_data.join() --output:-- h ello world #blank string returned when server closed socket hello world ------------------------ #client using read(): ----------------- require 'socket' port = 3030 socket = TCPSocket.new('localhost', port) all_data = [] while partial_data = socket.read(1012) puts partial_data all_data << partial_data end socket.close puts all_data.join() --output: -- hello world hello world ---------------- So the way I see it: 1) If the amount of data sent by the server is less than the size limit specified in read(), read() will block until the server closes the socket. Therefore, the server must close the socket. 2) If the amount of data sent by the server is more than the size limit specified in read(), and you just write: all_data = read(1012) puts data then you aren't reading all the data. You just get the first 1012 bytes. -- Posted via http://www.ruby-forum.com/.