<!--
{
  "availability" : [
    "iOS: 2.0.0 - 27.0.0",
    "iPadOS: 2.0.0 - 27.0.0",
    "macCatalyst: 13.1.0 - 27.0.0",
    "macOS: 10.2.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",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSNetService"
  },
  "title" : "NetService"
}
-->

# NetService

A network service that broadcasts its availability using multicast DNS.

```
class NetService
```

## Overview

The [`NetService`](/documentation/Foundation/NetService) class represents a network service, either one your application publishes or is a client of. This class and the [`NetServiceBrowser`](/documentation/Foundation/NetServiceBrowser) class use multicast DNS to convey information about network services to and from your application. The API of [`NetService`](/documentation/Foundation/NetService) provides a convenient way to publish the services offered by your application and to resolve the socket address for a service.

The types of services you access using [`NetService`](/documentation/Foundation/NetService) are the same types that you access directly using BSD sockets. HTTP and FTP are two services commonly provided by systems. (For a list of common services and the ports used by those services, see the file `/etc/services`.) Applications can also define their own custom services to provide specific data to clients.

You can use the [`NetService`](/documentation/Foundation/NetService) class as either a publisher of a service or a client of a service. If your application publishes a service, your code must acquire a port and prepare a socket to communicate with clients. Once your socket is ready, you use the [`NetService`](/documentation/Foundation/NetService) class to notify clients that your service is ready. If your application is the client of a network service, you can either create an [`NetService`](/documentation/Foundation/NetService) object directly (if you know the exact host and port information) or use an [`NetServiceBrowser`](/documentation/Foundation/NetServiceBrowser) object to browse for services.

To publish a service, initialize your [`NetService`](/documentation/Foundation/NetService) object with the service name, domain, type, and port information. All of this information must be valid for the socket created by your application. Once initialized, call the [`publish()`](/documentation/Foundation/NetService/publish()) method to broadcast your service information to the network.

When connecting to a service, use the [`NetServiceBrowser`](/documentation/Foundation/NetServiceBrowser) class to locate the service on the network and obtain the corresponding [`NetService`](/documentation/Foundation/NetService) object. Once you have the object, call the [`resolve(withTimeout:)`](/documentation/Foundation/NetService/resolve(withTimeout:)) method to verify that the service is available and ready for your application. If it is, the [`addresses`](/documentation/Foundation/NetService/addresses) property provides the socket information you can use to connect to the service.

The methods of [`NetService`](/documentation/Foundation/NetService) operate asynchronously so your application is not impacted by the speed of the network. All information about a service is returned to your application through the [`NetService`](/documentation/Foundation/NetService) object’s delegate. You must provide a delegate object to respond to messages and to handle errors appropriately.

## Topics

### Creating Network Services

[`init(domain:type:name:)`](/documentation/Foundation/NetService/init(domain:type:name:))

Returns the receiver, initialized as a network service of a given type and sets the initial host information.

[`init(domain:type:name:port:)`](/documentation/Foundation/NetService/init(domain:type:name:port:))

Initializes the receiver for publishing a network service of type `type` at the socket location specified by `domain`, `name`, and `port`.

### Configuring Network Services

[`data(fromTXTRecord:)`](/documentation/Foundation/NetService/data(fromTXTRecord:))

Returns an `NSData` object representing a TXT record formed from a given dictionary.

[`dictionary(fromTXTRecord:)`](/documentation/Foundation/NetService/dictionary(fromTXTRecord:))

Returns a dictionary representing a TXT record given as an `NSData` object.

[`addresses`](/documentation/Foundation/NetService/addresses)

A read-only array containing `NSData` objects, each of which contains a socket address for the service.

[`domain`](/documentation/Foundation/NetService/domain)

A string containing the domain for this service.

[`includesPeerToPeer`](/documentation/Foundation/NetService/includesPeerToPeer)

Specifies whether to also publish, resolve, or monitor this service over peer-to-peer Bluetooth and Wi-Fi, if available.

[`getInputStream(_:outputStream:)`](/documentation/Foundation/NetService/getInputStream(_:outputStream:))

Creates a pair of input and output streams for the receiver and returns a Boolean value that indicates whether they were retrieved successfully.

[`name`](/documentation/Foundation/NetService/name)

A string containing the name of this service.

[`type`](/documentation/Foundation/NetService/type)

The type of the published service.

[`txtRecordData()`](/documentation/Foundation/NetService/txtRecordData())

Returns the TXT record for the receiver.

[`setTXTRecord(_:)`](/documentation/Foundation/NetService/setTXTRecord(_:))

Sets the TXT record for the receiver, and returns a Boolean value that indicates whether the operation was successful.

[`delegate`](/documentation/Foundation/NetService/delegate)

The delegate for the receiver.

### Managing Run Loops

[`schedule(in:forMode:)`](/documentation/Foundation/NetService/schedule(in:forMode:))

Adds the service to the specified run loop.

[`remove(from:forMode:)`](/documentation/Foundation/NetService/remove(from:forMode:))

Removes the service from the given run loop for a given mode.

### Using Network Services

[`publish()`](/documentation/Foundation/NetService/publish())

Attempts to advertise the receiver’s on the network.

[`publish(options:)`](/documentation/Foundation/NetService/publish(options:))

Attempts to advertise the receiver on the network, with the given options.

[`resolve()`](/documentation/Foundation/NetService/resolve())

Starts a resolve process for the service.

[`resolve(withTimeout:)`](/documentation/Foundation/NetService/resolve(withTimeout:))

Starts a resolve process of a finite duration for the service.

[`port`](/documentation/Foundation/NetService/port)

The port on which the service is listening for connections.

[`startMonitoring()`](/documentation/Foundation/NetService/startMonitoring())

Starts the monitoring of TXT-record updates for the receiver.

[`stop()`](/documentation/Foundation/NetService/stop())

Halts a currently running attempt to publish or resolve a service.

[`stopMonitoring()`](/documentation/Foundation/NetService/stopMonitoring())

Stops the monitoring of TXT-record updates for the receiver.

### Obtaining the DNS Hostname

[`hostName`](/documentation/Foundation/NetService/hostName)

A string containing the DNS hostname for this service.

### Constants

[NSNetServices Errors](/documentation/Foundation/nsnetservices-errors)

If an error occurs, the delegate error-handling methods return a dictionary with the following keys.

[`NetService.ErrorCode`](/documentation/Foundation/NetService/ErrorCode-swift.enum)

These constants identify errors that can occur when accessing net services.

[`NetService.Options`](/documentation/Foundation/NetService/Options)

These constants specify options for a network service.



---

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)