main.m 1.29 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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#import <Foundation/Foundation.h>

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

@implementation MyPublisher

- (void)publishService:(UInt16)thePort
	{
	service = [[NSNetService alloc]
		initWithDomain:@"" type:@"_example._tcp." name:@"Cocoa Example" port:thePort];
	[service setDelegate:self];
	[service publish];
	}

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

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

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

@end

int main(int argc, char *argv[])
	{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

	// Get a new listening socket...
	int s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
	struct sockaddr_in sa = { sizeof(sa), AF_INET };
	int size = sizeof(sa);
	bind(s, (struct sockaddr *)&sa, sizeof(sa));
	getsockname(s, (struct sockaddr *)&sa, &size);

	// ... and advertise it
	[[[MyPublisher alloc]init] publishService: ntohs(sa.sin_port)];

	[[NSRunLoop currentRunLoop] run];
	[pool release];
	return 0;
	}