On 11/11/11 07:57, Duan, Jingjing wrote: > Have you tried the 'at_exit' hook? You should be able to close existing sockets in at_exit. > > -Jingjing > > > -----Original Message----- > From: Fengfeng Li [mailto:lifengfeng / huawei.com] > Sent: Thursday, November 10, 2011 1:51 AM > To: ruby-talk ML > Subject: How to kill an exist tcpserver or tcpsocket? > > Hi everyone, > > I¡Çm using the following codes to create and close a tcpsocket on > WindowsXP: > > def open > @tcp_server = TCPServer.new(@para[:host], @para[:port]) > @ch = @tcp_server.accept > end > ... > def close > @tcp_server.close > @ch.close > end > > They are very simple. My problem is, if I don't close()(sometimes my > users terminate my programe anyway before close() is called), the > TcpServer will be LISTENING and the tcpSocket will be CLOSE_WAIT. If I > call open() at this moment, ruby will raise Errno::EADDRINUSE. > So, I want to rescue this Exception and handle it, is there a way to > kill existed tcpServer/tcpSocket? > I searched for a long time, and only found apps like > killtcp.exe/aoi.exe, but they would be the last choice to me. Is there > some other way? Such as command line or ready-made methods? > > Thanks a lot. > > > Regards, > LiFengfeng > I'm not too familiar with the TCPServer class, but do you have access to the underlying socket options? You probably want to set SO_REUSEADDR on it so it can be recycled immediately. Sam