<!--
{
  "availability" : [

  ],
  "documentType" : "symbol",
  "framework" : "Tor",
  "identifier" : "/documentation/Tor/HostPort/localhost(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Tor"
    ],
    "preciseIdentifier" : "s:3Tor8HostPortV9localhostyACSiFZ"
  },
  "title" : "localhost(_:)"
}
-->

# localhost(_:)

Returns a loopback endpoint (`127.0.0.1:port`) for the given port.

```
static func localhost(_ port: Int) -> HostPort
```

## Parameters

`port`

TCP port on 127.0.0.1.

## Return Value

A fresh `HostPort` with `host == "127.0.0.1"`.

## Discussion

Convenience factory for the most common shape of `HostPort` inside
swift-tor: Tor’s SOCKS and control endpoints both live on the
loopback interface, and the overwhelming majority of callers never
need a non-loopback host. Using this factory makes it impossible
to typo `"127.0.0.1"` at call sites and keeps intent explicit.

> Note: This factory always returns an **IPv4** loopback. If you
> need IPv6 loopback (`::1`), construct the value explicitly via
> ``doc://Tor/documentation/Tor/HostPort/init(host:port:)``. The two loopback families are distinct at
> the kernel socket layer and will not unify on `connect(2)`.

> Important: The string literal `"localhost"` is deliberately **not**
> used here to avoid an unnecessary DNS lookup on Apple platforms.