Problems with TCPSocket

I am trying out a little code for a client/server and I was testing to
see how many simultaneous clients the server
could handle, but I always get a “Bad file descriptor - connect(2)”
error message, exactly after the sixth client connects. I have tried
it with my firewall disabled and I’m testing it on laptop that runs
Win XP. Any help would be much appreciated.

cacciatc

Here are my client and server:

class Client
def initialize(addr,p)
begin
@t = TCPSocket.new(addr,p)
print "connecting to " + addr.to_s + " on port " + p.to_s +
“…”
rescue
puts “error: #{$!}”
else
puts “connected”
end
end #end init

def safe_gets
begin
output = @t.gets
rescue
puts “error: #{$!}”
tear_down
else
return output
end
end #end safe gets

def safe_write(sock,msg)
begin
sock.write(msg)
rescue
@desc.delete(sock)
sock.close
end
end

def get_login
out1 = safe_gets
out2 = safe_gets
print out1.chomp
@name = gets
print out2.chomp
@passwd = gets
end #end get login

def connect
safe_write(@t,@name.chomp + " " + @passwd)
out = safe_gets
out
end #end connect

def run
get_login
if connect != “Welcome\n” then
puts “incorrect name or passwd”
get_login
if connect != “Welcome\n” then
tear_down
end
end
#processing loop
while 1
# puts @t.gets
print ">> "
input = gets
safe_write(@t,input)
if input.downcase == “q\n” then
tear_down
end
out = safe_gets
puts out
end
tear_down
end #end run

def tear_down
@t.close
exit
end # end tear down

end #end Client

c = Array.new(25)
cnt = 0
while cnt < 25
c.push(Client.new(“localhost”,10001))
i = 0
while i < 1000000
i+=1
end
cnt += 1
end
c.each {|cl| cl.run}
c.each {|cl| cl.tear_down}

class Logind
def initialize§
@t = TCPServer.new§
@t.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR,1)
@desc = Array.new
@gdesc = Hash.new
@desc.push(@t)
end

def accept_new_connection
begin
newsock = @t.accept
rescue Errno::EAGAIN, Errno::ECONNABORTED, Errno::EINTR
IO.select[@t]
retry
end
newsock.write(“username: \npasswd: \n”)
begin
input = safe_gets(newsock)
rescue
#log bad socket
puts newsock
newsock.close
else
safe_write(newsock,“Welcome\n”)
@desc.push(newsock)
end
end #end accept_new_connection

def run
while true
res = select(@desc,nil,nil,nil)
if res != nil then
for sock in res[0]
if sock == @t then
accept_new_connection
else
if sock.eof? then
sock.close
@desc.delete(sock)
else
input = safe_gets(sock)
end
end
end #end for
end #end select test
end #end while
end # end run

def safe_gets(sock)
ret_cnt = 0
begin
out = sock.gets
rescue
if ret_cnt < 5 then
ret_cnt+=1
retry
end
else
return out
end
end

def safe_write(sock,msg)
ret_cnt = 0
begin
sock.write(msg)
rescue
if ret_cnt < 5 then
ret_cnt+=1
retry
end
end
end

end #end logind

l = Logind.new(10001)
l.run