main.m 1.43 KB
Newer Older
O'Reilly Media, Inc. committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#import <Foundation/Foundation.h>

@interface MyResolver : NSObject
	{
	NSNetService *service;
	}
@end

@implementation MyResolver

- (void)resolveService
	{
	service = [[NSNetService alloc]
		initWithDomain:@"local." type:@"_example._tcp." name:@"Cocoa Example"];
	[service setDelegate:self];
	[service resolve];
	}

- (void)netServiceWillResolve:(NSNetService *)sender
	{
	NSLog(@"netServiceWillResolve: %@.%@%@\n",
		[sender name], [sender type], [sender domain]);
	}

- (void)netServiceDidResolveAddress:(NSNetService *)s
	{
	NSLog(@"DidResolve: %@.%@%@\n", [s name], [s type], [s domain]);

	// May get more than one reply
	NSArray *addresses = [s addresses];
	int count;
	for (count = 0; count < [addresses count]; count++)
		{
		char addr[256];
		struct sockaddr_in *sa = (struct sockaddr_in *)
			[[addresses objectAtIndex:count] bytes];
		if (inet_ntop(sa->sin_family, &sa->sin_addr, addr, sizeof(addr)))
			NSLog(@"DidResolve: %s:%d \n", addr, ntohs(sa->sin_port));
		}
	}

- (void)netService:(NSNetService *)sender didNotResolve:(NSDictionary *)errorDict
	{
	NSLog(@"didNotResolve: %@.%@%@\n",
	  [sender name], [sender type], [sender domain]);
	}

@end

int main(int argc, char *argv[])
	{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	[[[MyResolver alloc]init] resolveService];
	[[NSRunLoop currentRunLoop] run];
	[pool release];
	return 0;
	}