Class: NSNetService

Inherits:
NSObject show all

Overview

The NSNetService class represents a network service that your application publishes or uses as a client. 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.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from NSObject

#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version

Constructor Details

This class inherits a constructor from NSObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class NSObject

Class Method Details

+ (NSData) dataFromTXTRecordDictionary(txtDictionary)

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

Parameters:

  • txtDictionary (Hash)

    A dictionary containing a TXT record.

Returns:

  • (NSData)

    An NSData object representing TXT data formed from txtDictionary. Fails an assertion if txtDictionary cannot be represented as an NSData object.

+ (Hash) dictionaryFromTXTRecordData(txtData)

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

Parameters:

  • txtData (NSData)

    A data object encoding a TXT record.

Returns:

  • (Hash)

    A dictionary representing txtData. The dictionary’s keys are NSString objects using UTF8 encoding. The values associated with all the dictionary’s keys are NSData objects that encapsulate strings or data.Fails an assertion if txtData cannot be represented as an NSDictionary object.

Instance Method Details

- (Array) addresses

Returns an array containing NSData objects, each of which contains a socket address for the service. It is possible for a single service to resolve to more than one address or not resolve to any addresses. A service might resolve to multiple addresses if the computer publishing the service is currently multihoming.

Returns:

  • (Array)

    An array containing NSData objects, each of which contains a socket address for the service. Each NSData object in the returned array contains an appropriate sockaddr structure that you can use to connect to the socket. The exact type of this structure depends on the service to which you are connecting. If no addresses were resolved for the service, the returned array contains zero elements.

- (Object) delegate

Returns the delegate for the receiver.

Returns:

  • (Object)

    The delegate for the receiver.

- (String) domain

Returns the domain name of the service.

Returns:

  • (String)

    The domain name of the service.This can be an explicit domain name or it can contain the generic local domain name, @"local." (note the trailing period, which indicates an absolute name).

- (Boolean) getInputStream(inputStream, outputStream:outputStream)

Retrieves by reference the input and output streams for the receiver and returns a Boolean value that indicates whether they were retrieved successfully. After this method is called, no delegate callbacks are called by the receiver.

Parameters:

  • inputStream (Pointer)

    Upon return, the input stream for the receiver.

  • outputStream (Pointer)

    Upon return, the output stream for the receiver.

Returns:

  • (Boolean)

    YES if the streams are created successfully, otherwise NO.

- (String) hostName

Returns the host name of the computer providing the service.

Returns:

  • (String)

    The host name of the computer providing the service. Returns nil if a successful resolve has not occurred.

- (Object) initWithDomain(domain, type:type, name:name)

Returns the receiver, initialized as a network service of a given type and sets the initial host information. This method is the appropriate initializer to use to resolve a service—to publish a service, use initWithDomain:type:name:port:.If you know the values for domain, type, and name of the service you wish to connect to, you can create an NSNetService object using this initializer and call resolveWithTimeout: on the result. You cannot use this initializer to publish a service. This initializer passes an invalid port number to the designated initializer, which prevents the service from being registered. Calling publish on an NSNetService object initialized with this method generates a call to your delegate’s netService:didNotPublish: method with an NSNetServicesBadArgumentError error.

Parameters:

  • domain (String)

    The domain for the service. For the local domain, use @“local.” not @“”.

  • type (String)

    The network service type.type must contain both the service type and transport layer information. To ensure that the mDNS responder searches for services, as opposed to hosts, prefix both the service name and transport layer name with an underscore character (“_”). For example, to search for an HTTP service on TCP, you would use the type string “_http._tcp.”. Note that the period character at the end of the string, which indicates that the domain name is an absolute name, is required.

  • name (String)

    The name of the service to resolve.

Returns:

  • (Object)

    The receiver, initialized as a network service named name of type type in the domain domain.

- (Object) initWithDomain(domain, type:type, name:name, port:port)

Initializes the receiver as a network service of type type at the socket location specified by domain, name, and port. You use this method to create a service that you wish to publish on the network. Although you can also use this method to create a service you wish to resolve on the network, it is generally more appropriate to use the initWithDomain:type:name: method instead.When publishing a service, you must provide valid arguments in order to advertise your service correctly. If the host computer has access to multiple registration domains, you must create separate NSNetService objects for each domain. If you attempt to publish in a domain for which you do not have registration authority, your request may be denied.It is acceptable to use an empty string for the domain argument when publishing or browsing a service, but do not rely on this for resolution.This method is the designated initializer.

Parameters:

  • domain (String)

    The domain for the service. For the local domain, use @“local.” not @“”.It is generally preferred to use a NSNetServiceBrowser object to obtain the local registration domain in which to publish your service. To use this default domain, simply pass in an empty string (@“”).

  • type (String)

    The network service type.type must contain both the service type and transport layer information. To ensure that the mDNS responder searches for services, as opposed to hosts, prefix both the service name and transport layer name with an underscore character (“_”). For example, to search for an HTTP service on TCP, you would use the type string “_http._tcp.”. Note that the period character at the end of the string, which indicates that the domain name is an absolute name, is required.

  • name (String)

    The name by which the service is identified to the network. The name must be unique. If you pass the empty string (@“”), the system automatically advertises your service using the computer name as the service name.

  • port (Integer)

    The port on which the service is published.port must be a port number acquired by your application for the service.

Returns:

- (String) name

Returns the name of the service.

Returns:

  • (String)

    The name of the service.

- (Integer) port

Provides the port of the receiver.

Returns:

  • (Integer)

    The receiver’s port. -1 when it has not been resolved.

- (Object) publish

Attempts to advertise the receiver’s on the network. This method returns immediately, with success or failure indicated by the callbacks to the delegate.

Returns:

- (Object) publishWithOptions(serviceOptions)

Attempts to advertise the receiver on the network, with the given options. This method returns immediately, with success or failure indicated by the callbacks to the delegate.

Parameters:

  • serviceOptions (NSNetServiceOptions)

    Options for the receiver.

Returns:

- (Object) removeFromRunLoop(aRunLoop, forMode:mode)

Removes the service from the given run loop for a given mode. You can use this method in conjunction with scheduleInRunLoop:forMode: to transfer the service to a different run loop. Although it is possible to remove an NSNetService object completely from any run loop and then attempt actions on it, it is an error to do so.

Parameters:

  • aRunLoop (NSRunLoop)

    The run loop from which to remove the receiver.

  • mode (String)

    The run loop mode from which to remove the receiver. Possible values for mode are discussed in the "Constants" section of NSRunLoop.

Returns:

- (Object) resolveWithTimeout(timeout)

Starts a resolve process of a finite duration for the receiver. If the resolve succeeds before the timeout period lapses, the receiver sends netServiceDidResolveAddress: to the delegate. Otherwise, the receiver sends netService:didNotResolve: to the delegate.

Parameters:

  • timeout (NSTimeInterval)

    The maximum number of seconds to attempt a resolve. A value of 0.0 indicates no timeout and a resolve process of indefinite duration.

Returns:

- (Object) scheduleInRunLoop(aRunLoop, forMode:mode)

Adds the service to the specified run loop. You can use this method in conjunction with removeFromRunLoop:forMode: to transfer a service to a different run loop. You should not attempt to run a service on multiple run loops.

Parameters:

  • aRunLoop (NSRunLoop)

    The run loop to which to add the receiver.

  • mode (String)

    The run loop mode to which to add the receiver. Possible values for mode are discussed in the "Constants" section of NSRunLoop.

Returns:

- (Object) setDelegate(delegate)

Sets the delegate for the receiver. The delegate is not retained.

Parameters:

Returns:

- (Boolean) setTXTRecordData(recordData)

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

Parameters:

  • recordData (NSData)

    The TXT record for the receiver.

Returns:

  • (Boolean)

    YES if recordData is successfully set as the TXT record, otherwise NO.

- (Object) startMonitoring

Starts the monitoring of TXT-record updates for the receiver. The delegate must implement netService:didUpdateTXTRecordData:, which is called when the TXT record for the receiver is updated.

Returns:

- (Object) stop

Halts a currently running attempt to publish or resolve a service. This method results in the sending of a netServiceDidStop: message to the delegate.

Returns:

- (Object) stopMonitoring

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

Returns:

- (NSData) TXTRecordData

Returns the TXT record for the receiver.

Returns:

- (String) type

Returns the type of the service.

Returns:

  • (String)

    The type of the service.