Next Page > Hide TOC

NSHost Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/Foundation.framework
Availability
Available in Mac OS X v10.0 and later.
Companion guide
Declared in
NSHost.h
Related sample code

Overview

The NSHost class provides methods to access the network name and address information for a host. Instances of the NSHost class represent individual hosts on a network. Use NSHost objects to get the current host’s name and address and to look up other hosts by name or by address.

To create an NSHost object, use the currentHost, hostWithAddress:, or hostWithName: class methods (don’t use alloc and init). These methods use available network administration services (such as NetInfo or the Domain Name Service) to discover all names and addresses for the host requested. They don’t attempt to contact the host itself, however. This approach avoids untimely delays due to a host being unavailable, but it may result in incomplete information about the host.

An NSHost object contains all of the network addresses and names discovered for a given host by the network administration services. Each NSHost object typically contains one unique address, but it may have more than one name. If an NSHost object has more than one name, the additional names are variations on the same name, typically the basic host name plus the fully qualified domain name. For example, with a host name “sales” in the domain “anycorp.com”, an NSHost object can hold both the names “sales” and “sales.anycorp.com”.

The NSHost class maintains a cache of the NSHost objects it creates so that requests for an existing NSHost object return that object instead of creating a new one. Use the setHostCacheEnabled: method to turn the cache off, forcing lookup of hosts as they’re requested. You can also use the flushHostCache method to clear the cache of its entries so that subsequent requests look up the host information and create new instances.

Tasks

Creating Hosts

Getting Host Information

Comparing Hosts

Managing the Host Cache

Class Methods

currentHost

Returns an NSHost object representing the host the process is running on.

+ (NSHost *)currentHost

Return Value

NSHost object for the process’s host.

Availability
See Also
Related Sample Code
Declared In
NSHost.h

flushHostCache

Releases the cache of existing NSHost objects so subsequent requests for NSHost objects create new ones.

+ (void)flushHostCache

Discussion

NSHost objects that were retained before this method was invoked remain valid.

Availability
See Also
Declared In
NSHost.h

hostWithAddress:

Returns the NSHost with the Internet address address.

+ (NSHost *)hostWithAddress:(NSString *)address

Parameters
address

Network address to look up. For example, @"127.0.0.1" or @"fe80::1".

Return Value

Host for address.

Discussion

If caching is turned on and the cache already contains an NSHost object with address, returns that object. Otherwise, this method creates an instance and returns it.

Availability
See Also
Related Sample Code
Declared In
NSHost.h

hostWithName:

Returns a host with a specific name.

+ (NSHost *)hostWithName:(NSString *)hostname

Parameters
hostname

Name of the host to look up. Can be either a simple hostname, such as @"sales", or a fully qualified domain name, such as @"sales.anycorp.com".

Return Value

Host named hostname.

Discussion

If caching is turned on and the cache already contains an NSHost object with name, returns that object. Otherwise, this method creates a new instance and returns it.

Availability
See Also
Related Sample Code
Declared In
NSHost.h

isHostCacheEnabled

Indicates whether caching is turned on or off.

+ (BOOL)isHostCacheEnabled

Return Value

YES when caching is turned on; NO otherwise.

Availability
See Also
Declared In
NSHost.h

setHostCacheEnabled:

Specifies whether the receiver is to cache instances as it creates them to avoid creating duplicate instances.

+ (void)setHostCacheEnabled:(BOOL)cacheOn

Parameters
cacheOn

YES to turn on caching. NO to turn of caching.

Discussion

Caching is turned on by default.

This method doesn’t flush the cache. If you turn caching off and then back on, new requests for hosts use what was in the cache at the time caching was turned off. However, NSHost objects created while caching is turned off aren’t entered into the cache.

Availability
See Also
Declared In
NSHost.h

Instance Methods

address

Returns one of the network addresses of the receiver.

- (NSString *)address

Return Value

One of the network address for the receiver. For example, @"192.42.172.1" or @"fe80::1".

Availability
See Also
Related Sample Code
Declared In
NSHost.h

addresses

Returns all the network addresses of the receiver.

- (NSArray *)addresses

Return Value

All the network addresses of the receiver.

Availability
See Also
Declared In
NSHost.h

isEqualToHost:

Indicates whether the receiver represents the same host as another NSHost object.

- (BOOL)isEqualToHost:(NSHost *)host

Parameters
host

Host to compare the receiver to.

Return Value

YES when the receiver and host share at least one network address; NO otherwise.

Availability
See Also
Declared In
NSHost.h

name

Returns one of the hostnames of the receiver.

- (NSString *)name

Return Value

One of the hostnames of the receiver. Can be either a simple hostname, such as @"sales", or a fully qualified domain name, such as @"sales.anycorp.com".

Availability
See Also
Related Sample Code
Declared In
NSHost.h

names

Returns all the hostnames of the receiver.

- (NSArray *)names

Return Value

All the hostnames of the receiver.

Availability
See Also
Declared In
NSHost.h

Next Page > Hide TOC


© 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-03-24)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.