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 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

-- 
Posted via http://www.ruby-forum.com/.