basic WEBrick question

Discussion in 'Ruby' started by Mark Volkmann, Jan 20, 2006.

  1. I have the following basic code. When I run this it says "TCPServer
    Error: Address already in use - bind(2)". I can hit Ctrl-C to kill it,
    change the port to anything and rerun it and I still get that error
    message. I must be missing something really basic.

    require 'webrick'
    include WEBrick

    class AddServlet < HTTPServlet::AbstractServlet
    def do_GET(req, res)
    n1 =3D req.query['n1'].to_i
    n2 =3D req.query['n2'].to_i

    res['Content-Type'] =3D 'text/html'
    res.body =3D "The sum of #{n1} and #{n2} is #{n1 + n2}."
    end
    end

    server =3D HTTPServer.new:)Port=3D>2000)
    trap('INT') { server.shutdown }
    trap('TERM') { server.shutdown }
    server.mount("/add", AddServlet)
    server.start

    --
    R. Mark Volkmann
    Partner, Object Computing, Inc.
     
    Mark Volkmann, Jan 20, 2006
    #1
    1. Advertising

  2. Mark Volkmann

    Caleb Tennis Guest

    On Friday 20 January 2006 10:59, Mark Volkmann wrote:
    > I have the following basic code. When I run this it says "TCPServer
    > Error: Address already in use - bind(2)". I can hit Ctrl-C to kill it,
    > change the port to anything and rerun it and I still get that error
    > message. I must be missing something really basic.


    It works here on Linux. What's your platform, and are you sure you don't have
    something else already running on port 2000 ?
     
    Caleb Tennis, Jan 20, 2006
    #2
    1. Advertising

  3. On 1/20/06, Caleb Tennis <> wrote:
    > On Friday 20 January 2006 10:59, Mark Volkmann wrote:
    > > I have the following basic code. When I run this it says "TCPServer
    > > Error: Address already in use - bind(2)". I can hit Ctrl-C to kill it,
    > > change the port to anything and rerun it and I still get that error
    > > message. I must be missing something really basic.

    >
    > It works here on Linux.


    Thanks for checking it!

    > What's your platform


    I've been able to duplicate this under both Windows XP and Fedora Core 4 Li=
    nux.

    > and are you sure you don't have
    > something else already running on port 2000 ?


    I tried again a while ago and it's now working under Windows. Makes me
    think that for some reasons when I killed the server it didn't
    immediately free up the port. However, under Linux it's still saying
    the port is in use.

    Is there an easy way I can ask Linux for a list of all the ports in use?

    --
    R. Mark Volkmann
    Partner, Object Computing, Inc.
     
    Mark Volkmann, Jan 20, 2006
    #3
  4. Mark Volkmann

    Caleb Tennis Guest


    > I tried again a while ago and it's now working under Windows. Makes me
    > think that for some reasons when I killed the server it didn't
    > immediately free up the port. However, under Linux it's still saying
    > the port is in use.


    There's usually a setting you can use to tell the OS to allow you to reuse the
    port once it's closed. I don't have my Pickaxe in front of me, but I do have
    my camel book and in Perl there's a "Reuse" parameter that gets passed to new
    TCP Sockets to be able to reuse the port. I'm guessing Webrick has something
    similiar.

    > Is there an easy way I can ask Linux for a list of all the ports in use?


    "netstat -at" for TCP ports.
     
    Caleb Tennis, Jan 20, 2006
    #4
    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. Engineer
    Replies:
    6
    Views:
    668
    Jeremy Bowers
    May 1, 2005
  2. Replies:
    3
    Views:
    331
    Peter Retief
    Jan 9, 2005
  3. Lloyd Zusman
    Replies:
    2
    Views:
    432
  4. vasudevram

    Basic authentication with WEBrick?

    vasudevram, Sep 6, 2006, in forum: Ruby
    Replies:
    3
    Views:
    220
    vasudevram
    Sep 10, 2006
  5. Aaron Smith

    WEBrick::Daemon and WEBrick

    Aaron Smith, Mar 9, 2007, in forum: Ruby
    Replies:
    2
    Views:
    340
    Aaron Smith
    Mar 18, 2007
Loading...

Share This Page