El Martes, 12 de Enero de 2010, Iñaki Baz C.
escribió:
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.
I’ve found the cause of my issue. My ruby script listens into a pipe and
when
a message is received it’s sent via the TCPSocket.
Also there is a thread which does @socket.gets("\r\n") to receive the
responses. In this way I can I can send multiple requests to the server
without waiting for the responses. Instead the thread doing “gets” reads
the
responses, parses a request identifier and that’s all.
But in this case when calling socket.close the connection is not
terminated. A
simplified code:
require “socket”
@socket = TCPSocket.new(server, port)
Thread.new do
res = @socket.gets("\r\n")
end
@socket.close
sleep 120
When “@socket.close” is called it’s just closed “at Ruby level”, this
is, I
cannot write/read from it as I get “closed pipe” error (which makes
sense).
However the TCP connection remains open at OS level (in ESTABLISHED
status as
netstat shows in both the client and server).
But it’s really interesting (for me) the following fact:
Without the line @socket.close netstat output shows (during the sleep
120):
tcp 0 0 192.168.1.10:51112 88.231.79.226:5062 ESTABLISHED
23814/irb
But when running the script with @socket.close line then I see:
tcp 0 0 192.168.1.10:51112 88.231.79.226:5062 ESTABLISHED -
It’s like if the Ruby process (irb) has detached itself from the socket
(no
process pid in the netstat output for that connection), but the socket
remains
open at OS level.
Is it the expected behavior?
Thanks.