# Getting the port number of serve/servlet when running on an ephemeral port

**URL:** https://racket.discourse.group/t/getting-the-port-number-of-serve-servlet-when-running-on-an-ephemeral-port/4130
**Category:** Questions & Answers
**Created:** [March 4, 2026, 12:22pm UTC](https://racket.discourse.group/t/getting-the-port-number-of-serve-servlet-when-running-on-an-ephemeral-port/4130 "2026-03-04T12:22:42Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![hnmdijkema](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/hnmdijkema/32/2629_2.png) [@hnmdijkema](https://racket.discourse.group/u/hnmdijkema)
#### Post date: [March 4, 2026, 12:22pm UTC](https://racket.discourse.group/t/getting-the-port-number-of-serve-servlet-when-running-on-an-ephemeral-port/4130/1 "2026-03-04T12:22:42Z")

</div>

Dear all,

I've trouble finding this in the documentation.  
Suppose I start the racket web framework as follows:

```scheme
(serve/servlet
             (λ (req) (web-serve h req))
             #:listen-ip "127.0.0.1"
             #:port 0
             #:command-line? #t
             #:servlet-path ""
             #:stateless? #t
             #:servlet-regexp #rx"")
            )))

```

In this case the server will listen on a port the OS chooses.

How do I get the port number it listens on?

---

<div class="post-metadata">

### Author: ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)
#### Post date: [March 4, 2026, 12:51pm UTC](https://racket.discourse.group/t/getting-the-port-number-of-serve-servlet-when-running-on-an-ephemeral-port/4130/2 "2026-03-04T12:51:48Z")

</div>

The [serve procedure](https://docs.racket-lang.org/web-server-internal/web-server.html#%28def._%28%28lib._web-server%2Fweb-server..rkt%29._serve%29%29) accepts a confirmation channel which can give you the port. I don’t think it’s available for some of the higher-level forms, though.

Here’s an [example use from my code](https://github.com/benknoble/frosthaven-manager/blob/ceab98845a4b96784022954d54bd91686496b4c3/server.rkt#L274). It’s relatively easy to combine with dispatch/servlet to do what some of the other interfaces do.

It might be nice if the other interfaces had this ability though.

---

<div class="post-metadata">

### Author: ![hnmdijkema](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/hnmdijkema/32/2629_2.png) [@hnmdijkema](https://racket.discourse.group/u/hnmdijkema)
#### Post date: [March 7, 2026, 9:11am UTC](https://racket.discourse.group/t/getting-the-port-number-of-serve-servlet-when-running-on-an-ephemeral-port/4130/3 "2026-03-07T09:11:27Z")

</div>

Thanks Ben, I looked at your code and have managed to use the confirmation channel in my code. I just get the first value of it and that's the port number. Just a question:

```scheme
  (match (async-channel-get port-ch)
    [(? port-number? port) (values (~a "http://" (best-interface-ip-address) ":" port) stop)]
    [(? exn? e) (raise e)]))

```

What are you doing here exactly?

---

<div class="post-metadata">

### Author: ![LiberalArtist](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/liberalartist/32/151_2.png) [@LiberalArtist](https://racket.discourse.group/u/LiberalArtist)
#### Post date: [March 7, 2026, 11:48pm UTC](https://racket.discourse.group/t/getting-the-port-number-of-serve-servlet-when-running-on-an-ephemeral-port/4130/4 "2026-03-07T23:48:36Z")

</div>

The second case exists because the server might fail to start, in which case the channel will get an exception value instead of a port number. Here's an example of a program that will produce such an exception (as long as you run it with normal privileges):

```scheme
#lang racket
(require web-server/web-server
         racket/async-channel)

(define ach
  (make-async-channel))
(define stop
  (serve #:confirmation-channel ach
         #:port 80 ;<-- needs privileges
         #:dispatch void))
(begin0 (sync ach)
  (sleep 1)
  (stop))

```

---

<div class="post-metadata">

### Author: ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)
#### Post date: [March 8, 2026, 2:52pm UTC](https://racket.discourse.group/t/getting-the-port-number-of-serve-servlet-when-running-on-an-ephemeral-port/4130/5 "2026-03-08T14:52:49Z")

</div>

Philip covered the need to dispatch on whether we get a port or an exception. The only other thing I think you might be asking is "what is going on with that URL construction?"—the code that starts this server wants to tell the user 2 things:

- how to access the resulting website on a LAN (to play with friends); and
- how to stop the server.
