<!--
{
  "availability" : [
    "iOS: 9.0.0 -",
    "iPadOS: 9.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.11.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/URLSession/streamTask(withHostName:port:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSURLSession(im)streamTaskWithHostName:port:"
  },
  "title" : "streamTask(withHostName:port:)"
}
-->

# streamTask(withHostName:port:)

Creates a task that establishes a bidirectional TCP/IP connection to a specified hostname and port.

```
func streamTask(withHostName hostname: String, port: Int) -> URLSessionStreamTask
```

## Parameters

`hostname`

The hostname of the connection endpoint.

`port`

The port of the connection endpoint.

## Return Value

The new session stream task.

## Discussion

After you create the task, you must start it by calling its [`resume()`](/documentation/Foundation/URLSessionTask/resume()) method.

---

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)