A network service that broadcasts its availability using multicast DNS.
SDKs
- iOS 2.0+
- macOS 10.2+
- Mac Catalyst 13.0+
- tvOS 9.0+
Framework
- Foundation
Declaration
class NetService : NSObject
Overview
The Net
class represents a network service, either one your application publishes or is a client of. This class and the Net
class use multicast DNS to convey information about network services to and from your application. The API of Net
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 Net
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 Net
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 Net
class to notify clients that your service is ready. If your application is the client of a network service, you can either create an Net
object directly (if you know the exact host and port information) or use an Net
object to browse for services.
To publish a service, initialize your Net
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 Net
class to locate the service on the network and obtain the corresponding Net
object. Once you have the object, call the resolve(with
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 Net
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 Net
object’s delegate. You must provide a delegate object to respond to messages and to handle errors appropriately.