I'm having trouble with EOF

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

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.directory_port)
while (client = server.accept)
Thread.start do
message = “”
seconds_to_live = 2
seconds_to_live.times do |i|
a = client.recvfrom( 90000 )[0]
message << a
break if i == seconds_to_live
break if a == “”
puts “made it out of the loop!!!..not”

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”
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. In the same way as it works with my client
without me adding an EOF signal?

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.

On 12.06.12 04:47, roob noob wrote:

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?

Closing down socket connections usually helps - your server #recv will
raise an error or even return “”. I don’t remember exactly.

In other news: It helps to realize that sockets are stream-oriented and
not message oriented like 0mq or cod. If you want messaging, not just
data streams, I would use one of these libs. (Halfway to shameless self