> For the complete documentation index, see [llms.txt](https://xrho.gitbook.io/rho/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xrho.gitbook.io/rho/gateways/binding.md).

# Binding Ports

## What is Port Binding?

In the traditional stack, a server "binds" to a port (e.g., `0.0.0.0:80`). In Rho, a Gateway can bind to ports within its address space. Each port becomes a child address in the Router trie.

```cpp
Gateway gw;
gw.address = {3, 1, 1, 1};  // Device at 3.1.1.1

gw.bindPort(80);   // Reachable at [3, 1, 1, 1, 80]
gw.bindPort(443);  // Reachable at [3, 1, 1, 1, 443]
gw.bindPort(7777); // Reachable at [3, 1, 1, 1, 7777]
```

## How It Works

`bindPort()` creates a Station and hooks it into the Router:

```cpp
void bindPort(u32 port) {
    Station* station = new Station();
    NumericalAddress portAddr = address;
    portAddr.push(port);
    router.hook(station, portAddr);

    BoundPort bp;
    bp.port = port;
    bp.station = station;
    boundPorts.push(bp);
}
```

When a cart targets `[3, 1, 1, 1, 80]`, the Router's forward match walks:

* `3` → `1` → `1` → `1` → `80` → station found. Delivered.

## Listening on a Port

```cpp
// Bind the port
Station* httpPort = gw.bindPort(80);

// Listen for carts on this port
httpPort->onCart([](Cart& c) {
    printf("HTTP request: %s\n", c.payload.c_str());
    // Handle the request...
});
```

## Port Addressing

In Rho's address model, ports are just deeper address components. There's no semantic difference between a "port" and any other address level. The concept is a convention, not a protocol feature.

A "port" at `[3, 1, 1, 1, 80]` is the same thing as a "device" at `[3, 1, 1, 1]` — it's just one level deeper in the tree. You could have ports within ports:

```cpp
// App at [3, 1, 1, 1, 80, 1]  — app 1 on port 80
// App at [3, 1, 1, 1, 80, 2]  — app 2 on port 80
```

This makes the addressing system naturally extensible without any protocol changes.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xrho.gitbook.io/rho/gateways/binding.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
