From: "Jesús Gabriel y Galán" Date: 2010-01-12T20:37:50+09:00 Subject: Re: How to close a TCP socket? (TCPSocket#close doesn't close it) On Tue, Jan 12, 2010 at 12:26 PM, Iñaki Baz Castillo wrote: > El Martes, 12 de Enero de 2010, Lars Christensen escribió: >> On Tue, Jan 12, 2010 at 11:43 AM, Iñaki Baz Castillo wrote: >> > Hi, perhaps I miss something but when I close a TCP connection using >> > TCPSocket#close the connection remains as OS level for long seconds >> > (minutes). >> > >> > Be reading the doc: >> > >> >  IO#close >> > >> >  Closes ios and flushes any pending writes to the operating system. The >> >  stream is unavailable for any further data operations; an IOError is >> > raised if such an attempt is made. I/O streams are automatically closed >> > when they are claimed by the garbage collector. >> > >> > So, does it mean that the TCP connection is *really* terminated when the >> > garbage collector removes it (some seconds/minutes after calling >> > TCPSocket#close)? >> >> The TCP connection is terminated when you call TCPSocket#close. > > Yes, I'm realizing of it right now. There must be some error in my code which > prevents the socket from being closed. I must investigate it. > > >> > Unfortunatelly it's not valid for me, I need the capability to close a >> > TCP connection and open a new one without mantaining two parallel >> > connections. >> > >> > Any other way to really close a TCP connection at OS level? Thanks. >> >> Why do you need to do that? Your connection is in the TIME_WAIT state, >> which is exists for good reasons. There are no parallel connections. > > In my buggy script the connection remains ESTABLISHED, but as I said above it > must be my fault somewhere in the script. > > However, by trying just: > >  require "socket" >  s = TCPSocket.new(server, port) >  s.close > > After "s.close" the connection is ended totally, no TIME_WAIT status. Take a look at this tutorial: http://www.ssfnet.org/Exchange/tcp/tcpTutorialNotes.html There's a diagram explaining all the states for a TCPSocket, and explains the reaons for the TIME_WAIT status. Hope this helps, Jesus.