# IO select. Reconnect procedure

**URL:** <https://www.ruby-forum.com/t/io-select-reconnect-procedure/188344>\
**Category:** Ruby\
**Created:** [May 10, 2010, 3:44pm UTC](https://www.ruby-forum.com/t/io-select-reconnect-procedure/188344 "2010-05-10T15:44:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![walle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/5daacb/32.png) [@walle](https://www.ruby-forum.com/u/walle)\
**Post date:** [May 10, 2010, 3:44pm UTC](https://www.ruby-forum.com/t/io-select-reconnect-procedure/188344/1 "2010-05-10T15:44:16Z")

</div>

Hey, I have a minor problem with my IRC bot. It disconnects every while  
and then, often with a few days in between. My problem lies in the  
reconnecting procedure. The bot does not notice when it has been  
disconnected. I have tried Socket.closed?, time-out timer for the select  
IO. Nothing seems to work.

Any idea why?

//Walle

[/code]  
def loop()  
while !@connection.closed?  
ready = select([@connection, $stdin], nil, nil, 10)  
next if !ready  
ready[0].each do |message|  
if(message == $stdin)  
return if $stdin.eof  
message = $stdin.gets  
send(message)  
elsif(message == @connection)  
return if @connection.eof  
message = @connection.gets  
data\_response(message.strip)  
end  
end  
end  
raise Errno::EPIPE  
end  
[/code]

---

<div class="post-metadata">

**Author:** ![walle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/5daacb/32.png) [@walle](https://www.ruby-forum.com/u/walle)\
**Post date:** [May 10, 2010, 5:35pm UTC](https://www.ruby-forum.com/t/io-select-reconnect-procedure/188344/2 "2010-05-10T17:35:45Z")

</div>

Maybe this will help.

class TCPSocket  
def off?  
begin  
eof  
rescue  
Kernel.puts “#{ **FILE** }:#{ **LINE** } #{$!.inspect}”  
true  
end  
end  
end

def loop  
until connection.off?  
…  
end  
end

---

<div class="post-metadata">

**Author:** ![walle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/5daacb/32.png) [@walle](https://www.ruby-forum.com/u/walle)\
**Post date:** [May 10, 2010, 7:20pm UTC](https://www.ruby-forum.com/t/io-select-reconnect-procedure/188344/3 "2010-05-10T19:20:34Z")

</div>

Walle W. wrote:

> Hey, I have a minor problem with my IRC bot. It disconnects every while  
> and then, often with a few days in between. My problem lies in the  
> reconnecting procedure. The bot does not notice when it has been  
> disconnected. I have tried Socket.closed?, time-out timer for the select  
> IO. Nothing seems to work.
> 
> Any idea why?

Maybe you have to detect when it sends you an empty string? (that  
usually means the socket has closed). If it’s a bug report it though.  
-rp

---

<div class="post-metadata">

**Author:** ![walle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/5daacb/32.png) [@walle](https://www.ruby-forum.com/u/walle)\
**Post date:** [May 10, 2010, 7:15pm UTC](https://www.ruby-forum.com/t/io-select-reconnect-procedure/188344/4 "2010-05-10T19:15:10Z")

</div>

I will give it a go

thanks

---

<div class="post-metadata">

**Author:** ![walle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/5daacb/32.png) [@walle](https://www.ruby-forum.com/u/walle)\
**Post date:** [June 24, 2010, 7:57pm UTC](https://www.ruby-forum.com/t/io-select-reconnect-procedure/188344/5 "2010-06-24T19:57:37Z")

</div>

Walle W. wrote:

> It still happens, but very rarely… The bot kicks in again if I write  
> anything in the window($stdin).  
> Does anyone have any thoughts on this?

Yeah I think it’s a generic “problem” of TCP. If the other side has  
closed the socket (or died, or been rebooted), and you never write  
anything to the socket, you will never learn it is closed until the next  
time you write something.

[http://en.wikibooks.org/wiki/Ruby\_Programming/Reference/Objects/Socket#keepalive](http://en.wikibooks.org/wiki/Ruby_Programming/Reference/Objects/Socket#keepalive)

---

<div class="post-metadata">

**Author:** ![walle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/5daacb/32.png) [@walle](https://www.ruby-forum.com/u/walle)\
**Post date:** [June 24, 2010, 6:14pm UTC](https://www.ruby-forum.com/t/io-select-reconnect-procedure/188344/6 "2010-06-24T18:14:06Z")

</div>

It still happens, but very rarely… The bot kicks in again if I write  
anything in the window($stdin).  
Does anyone have any thoughts on this?

//Walle

---

<div class="post-metadata">

**Author:** ![walle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/5daacb/32.png) [@walle](https://www.ruby-forum.com/u/walle)\
**Post date:** [June 25, 2010, 11:09am UTC](https://www.ruby-forum.com/t/io-select-reconnect-procedure/188344/7 "2010-06-25T11:09:58Z")

</div>

Roger P. wrote:

> Walle W. wrote:
> 
> > It still happens, but very rarely… The bot kicks in again if I write  
> > anything in the window($stdin).  
> > Does anyone have any thoughts on this?
> 
> Yeah I think it’s a generic “problem” of TCP. If the other side has  
> closed the socket (or died, or been rebooted), and you never write  
> anything to the socket, you will never learn it is closed until the next  
> time you write something.
> 
> [Ruby Programming/Reference/Objects/Socket - Wikibooks, open books for an open world](http://en.wikibooks.org/wiki/Ruby_Programming/Reference/Objects/Socket#keepalive)

Oh, okey.  
Thank you for the insight.

//Walle
