S
Scott Cole
I'm running a server that sometimes encounters a
connection-reset-by-peer error when reading data from a client. I'm
calling IO.Select to verify that there are characters to be read, but
occasionally the "rescue" below is activated when I do my gets. I
presume this means the socket was closed after Select noticed there were
characters to be read but before I actually try to read them.
The client and server are in the same machine, so there's no cable to
pull out.
The messages of interest are sent every 30 seconds, and there might be 1
or 2 failures in a day. Running on Mac OS X 10.5.6.
The problem seems to occur when the client (below) connects to send a
message and another client connects before the first client's
transaction is complete. This transaction takes about 1 second. The
specified timeout is 120 seconds.
Is there anything obvious wrong with my code? I omitted quite a few
lines, I hope not too much.
Thanks-
Scott
######################### Server (client follows)
#########################
require 'gserver'
class ChatServer < GServer # Server class derived from GServer super
class
def initialize(*args)
super(*args)
# Keep a record of the client IDs allocated
# and the lines of chat
@@client_id = 0
@@chat = []
end
def serve(io) # Serve method handles connections
# Increment the client ID so each client gets a unique ID
@@client_id += 1
my_client_id = @@client_id
my_position = @@chat.size
loop do
#Every n seconds check for data
n = 0.5
selection = IO.select([io], nil, [io], n)
# If some event occurred, retrieve the data and process it...
if selection && selection[0] then
# There was a read event
begin
line = io.gets
rescue Exception => e
stat = ''
puts "\nerror reading 'line' from #{io}"
puts "#{ e } (#{ e.class })!"
# Close socket
io.close
print("\nSelection is #{selection[0]}\n")
end
if selection[2].size > 0 then
puts "%%%%%%%% Select error array is #{selection[2]}" +
Time.now.to_s
end
connection-reset-by-peer error when reading data from a client. I'm
calling IO.Select to verify that there are characters to be read, but
occasionally the "rescue" below is activated when I do my gets. I
presume this means the socket was closed after Select noticed there were
characters to be read but before I actually try to read them.
The client and server are in the same machine, so there's no cable to
pull out.
The messages of interest are sent every 30 seconds, and there might be 1
or 2 failures in a day. Running on Mac OS X 10.5.6.
The problem seems to occur when the client (below) connects to send a
message and another client connects before the first client's
transaction is complete. This transaction takes about 1 second. The
specified timeout is 120 seconds.
Is there anything obvious wrong with my code? I omitted quite a few
lines, I hope not too much.
Thanks-
Scott
######################### Server (client follows)
#########################
require 'gserver'
class ChatServer < GServer # Server class derived from GServer super
class
def initialize(*args)
super(*args)
# Keep a record of the client IDs allocated
# and the lines of chat
@@client_id = 0
@@chat = []
end
def serve(io) # Serve method handles connections
# Increment the client ID so each client gets a unique ID
@@client_id += 1
my_client_id = @@client_id
my_position = @@chat.size
loop do
#Every n seconds check for data
n = 0.5
selection = IO.select([io], nil, [io], n)
# If some event occurred, retrieve the data and process it...
if selection && selection[0] then
# There was a read event
begin
line = io.gets
rescue Exception => e
stat = ''
puts "\nerror reading 'line' from #{io}"
puts "#{ e } (#{ e.class })!"
# Close socket
io.close
print("\nSelection is #{selection[0]}\n")
end
if selection[2].size > 0 then
puts "%%%%%%%% Select error array is #{selection[2]}" +
Time.now.to_s
end