TCPserver client disconnect

N

Noah

ruby newbie question...

How can I detect when a client disconnects from a TCPserver?
I'm streaming data to the client in a while loop (Shoutcast-style mp3
stream), but I'm not sure what condition to test to see if the client drops
out.

Thanks
 
J

Joel VanderWerf

Noah said:
ruby newbie question...

How can I detect when a client disconnects from a TCPserver?
I'm streaming data to the client in a while loop (Shoutcast-style mp3
stream), but I'm not sure what condition to test to see if the client drops
out.

Does sample/svr.rb (in the ruby distribution) help?
 
N

Noah

Joel VanderWerf said:
Does sample/svr.rb (in the ruby distribution) help?
Not really, because in that example, the server is checking for the client
eof, right? (Correct me if I'm wrong. I just started with Ruby a few days
ago.)

In my application, the client makes one initial request, and then simply
receives. The server is the only one sending data. It's like a HTTP
request. Imagine a client does a GET on a very large file. The server
begins to send the data, but the client drops out half-way through. How can
the server detect this and cancel the transfer?
 
J

Joel VanderWerf

Noah said:
Not really, because in that example, the server is checking for the client
eof, right? (Correct me if I'm wrong. I just started with Ruby a few days
ago.)

In my application, the client makes one initial request, and then simply
receives. The server is the only one sending data. It's like a HTTP
request. Imagine a client does a GET on a very large file. The server
begins to send the data, but the client drops out half-way through. How can
the server detect this and cancel the transfer?

Errno::ECONNRESET seems to be the exception raised. At least on linux.

In samples/svr.rb, I replaced

s.write(str)

with

s.write(str*10_000)

and ran samples/clnt.rb as usual. When the client is interrupted, the
server receives:

svr.rb:27:in `write': Connection reset by peer (Errno::ECONNRESET)
from svr.rb:27
from svr.rb:15:in `each'
from svr.rb:15
from svr.rb:12:in `loop'
from svr.rb:12
 
N

Noah

Joel VanderWerf said:
Errno::ECONNRESET seems to be the exception raised. At least on linux.

In samples/svr.rb, I replaced

s.write(str)

with

s.write(str*10_000)

and ran samples/clnt.rb as usual. When the client is interrupted, the
server receives:

svr.rb:27:in `write': Connection reset by peer (Errno::ECONNRESET)
from svr.rb:27
from svr.rb:15:in `each'
from svr.rb:15
from svr.rb:12:in `loop'
from svr.rb:12
Ah, it turns out I was generating that exception, but I couldn't see it
because it was jumping to an 'ensure' block, and I guess the thread would
just die. I moved the s.close into the ensure block and now it's working
great.

Thanks for the help.
 
J

Joel VanderWerf

Noah said:
Ah, it turns out I was generating that exception, but I couldn't see it
because it was jumping to an 'ensure' block, and I guess the thread would
just die. I moved the s.close into the ensure block and now it's working
great.

In case you haven't come across it yet:

Thread.abort_on_exception = true

will abort the whole process, instead of just killing the thread
quietly. Good for debugging...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top