Class

NSNetService

A network service that broadcasts its availability using multicast DNS.

Declaration

@interface NSNetService : NSObject

Overview

The NSNetService class represents a network service, either one your application publishes or is a client of. This class and the NSNetServiceBrowser class use multicast DNS to convey information about network services to and from your application. The API of NSNetService 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 NSNetService 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 NSNetService 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 NSNetService class to notify clients that your service is ready. If your application is the client of a network service, you can either create an NSNetService object directly (if you know the exact host and port information) or use an NSNetServiceBrowser object to browse for services.

To publish a service, initialize your NSNetService 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 method to broadcast your service information to the network.

When connecting to a service, use the NSNetServiceBrowser class to locate the service on the network and obtain the corresponding NSNetService object. Once you have the object, call the resolveWithTimeout: method to verify that the service is available and ready for your application. If it is, the addresses property provides the socket information you can use to connect to the service.

The methods of NSNetService 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 NSNetService object’s delegate. You must provide a delegate object to respond to messages and to handle errors appropriately.

Topics

Creating Network Services

- initWithDomain:type:name:

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

- initWithDomain: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

+ dataFromTXTRecordDictionary:

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

+ dictionaryFromTXTRecordData:

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

addresses

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

domain

A string containing the domain for this service.

includesPeerToPeer

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

- 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

A string containing the name of this service.

type

The type of the published service.

- TXTRecordData

Returns the TXT record for the receiver.

- setTXTRecordData:

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

- protocolSpecificInformation

Returns protocol specific information for legacy ZeroConf-style clients.

- setProtocolSpecificInformation:

Sets protocol specific information for legacy ZeroConf-style clients.

delegate

The delegate for the receiver.

Managing Run Loops

- scheduleInRunLoop:forMode:

Adds the service to the specified run loop.

- removeFromRunLoop:forMode:

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

Using Network Services

- publish

Attempts to advertise the receiver’s on the network.

- publishWithOptions:

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

- resolve

Starts a resolve process for the service.

Deprecated
- resolveWithTimeout:

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

port

The port on which the service is listening for connections.

- startMonitoring

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

- stop

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

- stopMonitoring

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

Obtaining the DNS Hostname

hostName

A string containing the DNS hostname for this service.

Constants

NSNetServices Errors

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

NSNetServicesError

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

NSNetServiceOptions

These constants specify options for a network service.

Relationships

Inherits From

See Also

Local Network Services

NSNetServiceDelegate

The interface a net service uses to inform its delegate about the state of the service it offers.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software