Newbie dumb question regarding GServer

Discussion in 'Ruby' started by Mike Pence, Sep 28, 2005.

  1. Mike Pence

    Mike Pence Guest

    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=3D10001, *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 =3D TimeServer.new
    server.audit =3D 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
    Mike Pence, Sep 28, 2005
    #1
    1. Advertising

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

    > 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
    James Edward Gray II, Sep 28, 2005
    #2
    1. Advertising

  3. --MGYHOYXEY6WxJCY8
    Content-Type: text/plain; charset=us-ascii
    Content-Disposition: inline

    On Thu, Sep 29, 2005 at 02:11:01AM +0900, Mike Pence wrote:
    > Why is the server stopping?


    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

    --MGYHOYXEY6WxJCY8
    Content-Type: application/pgp-signature; name="signature.asc"
    Content-Description: Digital signature
    Content-Disposition: inline

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)

    iD8DBQFDOtpynhUz11p9MSARAhIEAJ4mz3RKsMc6Hecv7AiCMdimarrUYwCg34Vj
    PoWplggCCXOM2kckBghC6Xw=
    =V+zf
    -----END PGP SIGNATURE-----

    --MGYHOYXEY6WxJCY8--
    Edward Faulkner, Sep 28, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Edward Gray II

    Thinking About GServer

    James Edward Gray II, Nov 19, 2004, in forum: Ruby
    Replies:
    4
    Views:
    113
    Joel VanderWerf
    Nov 19, 2004
  2. XiaoLiang Liu
    Replies:
    6
    Views:
    121
    XiaoLiang Liu
    Mar 26, 2008
  3. Iñaki Baz Castillo
    Replies:
    2
    Views:
    115
    Iñaki Baz Castillo
    Mar 27, 2008
  4. Victor Reyes

    TCP Server/GServer

    Victor Reyes, May 24, 2008, in forum: Ruby
    Replies:
    3
    Views:
    147
    Victor Reyes
    May 27, 2008
  5. Jerry C.
    Replies:
    8
    Views:
    222
    Uri Guttman
    Nov 23, 2003
Loading...

Share This Page