<!--
{
  "availability" : [
    "iOS: 2.0.0 - 27.0.0",
    "iPadOS: 2.0.0 - 27.0.0",
    "macCatalyst: 13.1.0 - 27.0.0",
    "macOS: 10.5.0 - 27.0.0",
    "tvOS: 9.0.0 - 27.0.0",
    "visionOS: 1.0.0 - 27.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NetService/port",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Property",
  "symbol" : {
    "kind" : "Instance Property",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSNetService(py)port"
  },
  "title" : "port"
}
-->

# port

The port on which the service is listening for connections.

```
var port: Int { get }
```

## Discussion

If the object was initialized by calling [`init(domain:type:name:port:)`](/documentation/Foundation/NetService/init(domain:type:name:port:)) (whether by your code or by a browser object), then the value was set when the object was first initialized.

If the object was initialized by calling [`init(domain:type:name:)`](/documentation/Foundation/NetService/init(domain:type:name:)), the value of this property is not valid (`-1`) until after the service has successfully been resolved (when `addresses` is non-`nil`).

> Backward Compatibility Note:
> This became a property in OS X v10.9 and iOS 7, but the underlying getter method (`port`) has been available since this class was first introduced.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)