Newbie dumb question regarding GServer

Hello all,

Great to be here. Ruby is re-igniting my love of software development.

I am running this code:

require 'gserver'

···

#
  # A server that returns the time in seconds since 1970.
  #
  class TimeServer < GServer
    def initialize(port=10001, *args)
      super(port, *args)
    end
    def serve(io)
      io.puts(Time.now.to_i)
    end
  end

  # Run the server with logging enabled (it's a separate thread).
  server = TimeServer.new
  server.audit = true # Turn logging on.
  server.start

And getting this output:

[Wed Sep 28 12:40:24 2005] TimeServer 127.0.0.1:10001 start
[Wed Sep 28 12:40:28 2005] TimeServer 127.0.0.1:10001 stop

Why is the server stopping?

Thanks,
Mike Pence

Hello all,

Howdy.

Great to be here. Ruby is re-igniting my love of software development.

Glad to hear it.

I am running this code:

require 'gserver'

  #
  # A server that returns the time in seconds since 1970.
  #
  class TimeServer < GServer
    def initialize(port=10001, *args)
      super(port, *args)
    end
    def serve(io)
      io.puts(Time.now.to_i)
    end
  end

  # Run the server with logging enabled (it's a separate thread).
  server = TimeServer.new
  server.audit = true # Turn logging on.
  server.start

We should probably update that example in the documentation. I'm sure we could do better.

And getting this output:

[Wed Sep 28 12:40:24 2005] TimeServer 127.0.0.1:10001 start
[Wed Sep 28 12:40:28 2005] TimeServer 127.0.0.1:10001 stop

Why is the server stopping?

GServer runs in its own Threads. But your main program is just starting it and then ending. You need to ask it to wait on the server. Add the following line at the end of your script:

server.join # wait on server...

Hope that helps. Welcome to Ruby!

James Edward Gray II

···

On Sep 28, 2005, at 12:11 PM, Mike Pence wrote:

If your main threads reaches the end, the program will exit. I think
you need to call GServer#join to make the main thread wait.

regards,
Ed

···

On Thu, Sep 29, 2005 at 02:11:01AM +0900, Mike Pence wrote:

Why is the server stopping?