TCP sockets are streams, not messages, so berkeley sockets send() function on some systems may send less data than required. Since Ruby Socket is very thin wrapper over berkeley sockets, AFAIK Socket#send will behave exactly like berkeley sockets send(). So what is the correct way to send a complete message via Ruby TCP sockets? In python it’s a special function for that called sendall(). But in Ruby i need to manually write code like that:
while (sent = sock.send( data, 0 ) < data.length do
data = data[ sent..-1 ]
end
To expand on what danielnegri says,
IO.writeends up callingio_binwriteinio.cThe relevant bit of the ruby source is below (
nandlenare initially set to the length of your data andoffsetto 0)As you can see, until it has written all the data it will keep on doing
goto retryand trying again.rb_io_wait_writablebasically checks that the value oferrnois such that one should try again (as opposed to something more fatal) and then callsselectto avoid busy-waiting.