GServer接收消息

时间:2009-03-10 19:04:05

标签: ruby

我使用下面的代码创建一个TCP / IP服务器,然后使用客户端代码来调用它,但由于某种原因,服务器没有收到客户端发送的消息。请指教。感谢

服务器:

class MyServer < GServer
  def initialize(port=20607, host=GServer::DEFAULT_HOST)
    super(port, host, Float::MAX, $stderr, true)
  end

  def serve(sock)
    begin
      @clients << sock
      stock.accept
      message = sock.gets.chomp
      puts "message: " << message
      this.stop if message == "#quitserver"
      # TODO: code that process commmands comes here
    rescue

    ensure
      @clients.delete(sock)
    end
  end
end

客户端:

require 'socket'
streamSock = TCPSocket.new( "127.0.0.1", 20607 )
puts "Socket created..." unless streamSock.nil?
sleep 5
streamSock.puts "hello world"
sleep 5
streamSock.close
puts "Socket closed."

1 个答案:

答案 0 :(得分:0)

oops我的错误,@ clients数组未声明。现在工作:)