From: Aaron Turner Date: 2010-01-13T07:06:11+09:00 Subject: Re: TCPSocket doesn't detect remote disconnection inmediatelly On Tue, Jan 12, 2010 at 1:21 PM, Iñaki Baz Castillo wrote: > El Martes, 12 de Enero de 2010, Brian Candler escribió: >> Iñaki Baz Castillo wrote: >> > Hi, I'm experimenting an annoying issue when a server disconnects a TCP >> > connection established by a Ruby client. >> >> Try writing a C program which uses the Unix socket API directly. It will >> be instructive. The book "Unix Network Programming (vol 1)" by the late >> Richard Stevens is an excellent authoritative tome. > > I'll do it. But it would be great if at least I could know in advance if what > I described is the expected behavior :) Long story short, what you're seeing with Ruby is not a bug, but rather how TCP sockets work. TCP sockets are bi-directional and when one side "closes" the connection, they are not actually causing the TCP session to tear down as telnet implies, but rather is informing the other end of the connection that they have no more data to send. The receive direction is still technically open via TCP, although the HTTP protocol specifies no more communication is possible. I also recommend "Unix Network Programming (vol 1)" by Stevens if you want to understand the details of sockets and network communication. Since network programming utilizes much of the same API constructs as file IO, many newbie network programmers make these sorta mistakes because they expect network sockets to behave like file descriptors when they do not. -- Aaron Turner http://synfin.net/ http://tcpreplay.synfin.net/ - Pcap editing and replay tools for Unix & Windows Those who would give up essential Liberty, to purchase a little temporary Safety, deserve neither Liberty nor Safety. -- Benjamin Franklin "carpe diem quam minimum credula postero"