2010-04-26 3 views
0

나는 TCP 소켓 서버 (루비)를 만들고있다. 연결된 각 클라이언트에 대해 스레드가 생성됩니다. 어떤 시점에서 모든 연결된 클라이언트에게 데이터를 보내려고합니다. 시도하는 동안 예외가 발생하면 스레드가 중단됩니다. (ruby 1.8.7)Ruby 소켓 서버 스레드 질문 : 모든 클라이언트에게 보내는 방법?

require 'socket' 
# I test it home right now 
server = TCPServer.new('localhost', 12345); 
while(session = server.accept) 
#Here is the thread being created 
Thread.new(session) do |s| 
    while(msg = s.gets) 
    #Here is the part that causes the error 
    Thread.list.each { |aThread| 
    if aThread != Thread.current 
    #So what I want it to do is to echo the message from one client to all others 
    #But for some reason it doesn't, and aborts on the following string 
    aThread.print "#{msg}\0" 
    end 
    } 
    end 
end 
Thread.abort_on_exception = true 
end 

무엇이 잘못 되었나요?

+0

어떤 예외가 있습니까? – Thomas

+0

프로그램은 설명없이 간단하게 중단됩니다. – Paul

+0

'accept'는 배열을 반환하므로 'while (session, session_sockaddr = server.accept)'을 사용하여 소켓을 sockaddr 문자열과 명확하게 구분하지 않으시겠습니까? – Fred

답변

0

다음 PDF에서는 루비의 소켓 프로그래밍에 대해 자세히 설명합니다. 비동기 처리 기능을 포함하며 작은 채팅 프로그램의 기능 데모를 통해 진행됩니다.