So I have a client server program, or at least that is what I am working
on. I don't want recv to return before the entire amount of data I want
to send has been sent. Here is how I am doing that on the client side:
message = "whatever"
server = TCPSocket.open(directory_address, directory_port)
server.send(message, 0)
seconds_to_live = 60
message = ""
seconds_to_live.times do |i|
message << socket.recvfrom( 90000 )[0]
break if server.recvfrom(1)[0] == ""
break if i == seconds_to_live
sleep(1)
end
This works great, but if I do the same thing on the server, it never
exits the loop. The following code fails:
server = TCPServer.open(@settings.port)
while (client = server.accept)
Thread.start do
message = ""
seconds_to_live = 2
seconds_to_live.times do |i|
message << client.recvfrom( 90000 )[0]
break if i == seconds_to_live
break if client.recvfrom(1)[0] == ""
sleep(1)
end
puts "made it out of the loop!!!"
after *much* debugging and testing etc, I have come to the conclusion
that this is because the client does not send an empty string to signal
EOF, like the server does. At least this is what I currently think, my
brain is a bit tired after trying to solve this all day, it has proven
quite tricky for me. The reason I think this is because the following
server code works:
server = TCPServer.open(@settings.directory_port)
loop do
Thread.start(server.accept) do |client|
message = ""
loop do
a = client.recvfrom(1)[0]
message << a
puts message
break if a == "r"
end
puts "OMG The loop is broken!"
the message is still entirely what I want it to be, and it breaks on the
"r" in whatever (remember the client code?), and then gets to puts. This
is working for me, although I do still need to add the time to live code
to it that is irrelevant for my question. The thing is, the server will
not always be sent whatever so I can't very well break on "r" all the
time. I could have a special character or string that the server
recognizes as EOF, but it would be SO SO SO SO much nicer if it broke on
"" like the client does, automatically.
Is there anyway for me to get this sort of behavior without having to
add my own EOF signal, like I did with the client?
Thank you so so so so much for any help on this, I have made it this far
but the debugging process on this one has left me mentally exhausted
lol, even finding what the problem was took me several hours of focusing
on it.
···
--
Posted via http://www.ruby-forum.com/.