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

···

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

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

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

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

···

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

-----Messaggio originale-----

···

Da: Sam Duncan [mailto:sduncan@wetafx.co.nz]
Inviato: giovedì 10 novembre 2011 20:19
A: ruby-talk ML
Oggetto: Re: How to kill an exist tcpserver or tcpsocket?

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

--
Caselle da 1GB, trasmetti allegati fino a 3GB e in piu' IMAP, POP3 e SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f

Sponsor:
Riccione Hotel 3 stelle in centro: Pacchetto Capodanno mezza pensione, animazione bimbi, zona relax, parcheggio. Scopri l'offerta solo per oggi...
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid983&d)-12