Hi I am writing some networking code and am having a heck of a time
figuring out how to break on EOT. I was sending an ASCII control
character, but this doesn’t work when I send binary data because
sometimes it is seen as the EOT character so breaks early. Here is one
thing I have tried:
server = TCPServer.open(address, port)
loop do
Thread.start(server.accept) do |client|
message = “”
loop do
message << client.recvfrom( 4 )[0]
break if client.eof? == true
end
yield(message, client)
end
end
if I replace the break if client.eof? == true with simply puts
client.eof? it will return false if it is not the end of what is being
sent, otherwise it just hangs there indefinitely. This is only one of
many ways I have tried, client.recvfrom does not seem to return 0 or -1
when no more data is to be sent, it also does not seem to return an
empty string. Pretty much the only way I have gotten this to work is by
looping client.recvfrom and then breaking if the character at -1 in what
it gets is the ASCII control character for EOT. Unfortunately I can not
use that because when I send random binary data there is a chance it
will end on EOT without it being a control character. Everything else I
try ends up just hanging indefinitely and never making it to the yield
statement, sometimes it is just waiting for more data to recvfrom other
times I have no idea what happens such as when it hangs at client.eof?
when it is actually the end of what is being sent but returns false
otherwise.
Surely there is some easy way to do this, but none of the Ruby
documentation mentions anything about how to and most of the examples do
not even have recv in a loop for some reason.