Hanging in a client server threaded program

I was having a buffered IO problem which I solved using IO.ready?, but
now I’m hanging. What’s interesting is that I wrote a little test
program to demonstrate the problem, and it doesn’t cause the server to
hang, but the larger program does. The object that communicates with
the server is exactly the same in both programs, except for some
trivialites that I had to change in order to make the program work
standalone. I use threads in the test program because the main program
uses them and if that is the problem I was hoping it would show up. The
server is hanging right after the “socket recieved” output in the
accept_new_connection method.

################## Test Reciever ###############
class Reciever
def initialize
@target = Target.new

@target.socket = TCPSocket.new(‘127.0.0.1’, 7824)
puts “Socket Opened”
@target.socket.write(“name”)
end

def run
while 1
if @target.socket.ready?
print “message recieved\n”
if @target.socket.eof?
@target.socket.close
raise :ServerClosedConnection
else
puts “Entered else”
message = @target.socket.read
@target.parse_and_execute(message)
end
end
end
end
end

class Target
attr_accessor :socket

def initialize
@socket
end

def parse_and_execute(message)
puts message
end
end
thr = Array.new
thr << Thread.new do
Reciever.new.run
end
thr << Thread.new do
sleep()
end
thr.each {|thread| thread.join}

################## Actual Reciever, This one causes the server to hang
#######
class Reciever
def initialize(target)
ip_address = nil

target.socket = TCPSocket.new(ip_address, $port)
puts “Socket Opened”
name = target.me.name
puts "Name is " + target.me.name.to_s
target.socket.write(“Raj”)
puts “Name sent”
end

def run(target)
while 1
if target.socket.ready?
print “message recieved\n”
if target.socket.eof?
target.socket.close
raise :ServerClosedConnection
else
message = target.socket.read
target.parse_and_execute(message)
end
end
end
end
end

################## Server ###################
class Interlink
attr_accessor :descriptors

def initialize(server)
@descriptors = Array.new
@server = server
@serverSocket = server.socket = TCPServer.new(‘localhost’, $port)
@serverSocket.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1)
@descriptors << @serverSocket
end

def run
while 1
res = select(@descriptors, nil, nil, nil)
unless res.nil?
res[0].each do |sock|
if sock == @serverSocket
accept_new_connection
else
if sock.eof?
puts “Socket #{sock} disconnected.”
sock.close
else
#broadcast(sock.gets, sock)
end
end
end
end
end
end

def accept_new_connection
newsock = @serverSocket.accept
print “Socket accepted\n”
name = newsock.recv(20) #####THE SERVER IS HANGING HERE#######
print “name recieved\n”
print “name is #{name}\n”
@server.add_player(name, newsock)
print “player added\n”
@descriptors << newsock
newsock.write(“test target data”)
#newsock.write(“info You connected to the STCCG GameServer.\n”)
#broadcast(“info #{name} joined the game.\n”, newsock)
end

def broadcast(str, omit_sock)
@descriptors.each do |socket|
if socket != @serverSocket and socket != omit_sock
socket.write(str)
end
end
end
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs