# Basic WEBrick question

**URL:** <https://www.ruby-forum.com/t/basic-webrick-question/95850>\
**Category:** Ruby\
**Created:** [January 20, 2006, 5:38pm UTC](https://www.ruby-forum.com/t/basic-webrick-question/95850 "2006-01-20T17:38:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 20, 2006, 5:38pm UTC](https://www.ruby-forum.com/t/basic-webrick-question/95850/1 "2006-01-20T17:38:17Z")

</div>

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 = req.query[‘n1’].to\_i  
n2 = req.query[‘n2’].to\_i

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

```

end  
end

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

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 20, 2006, 6:50pm UTC](https://www.ruby-forum.com/t/basic-webrick-question/95850/2 "2006-01-20T18:50:51Z")

</div>

On Friday 20 January 2006 10:59, Mark V. 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 ?

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 20, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/basic-webrick-question/95850/3 "2006-01-20T19:57:24Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [January 20, 2006, 7:48pm UTC](https://www.ruby-forum.com/t/basic-webrick-question/95850/4 "2006-01-20T19:48:20Z")

</div>

On 1/20/06, Caleb T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Friday 20 January 2006 10:59, Mark V. 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  
Linux.

> 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?
