* Fixed #9: compilation error with iPhone 3.0 SDK.
* MYBonjourRegistration now allows you to set a TXT dictionary with non-NSData key values; they'll be translated to UTF-8 object descriptions. Useful for NSStrings and NSNumbers.
4 // Created by Jens Alfke on 1/4/08.
5 // Copyright 2008 Jens Alfke. All rights reserved.
10 #import "MYPortMapper.h"
18 /** A trivial class that just demonstrates how to create a MYPortMapper and listen for changes. */
19 @interface MYPortMapperTest : NSObject
21 MYPortMapper *_mapper;
27 @implementation MYPortMapperTest
34 // Create MYPortMapper. This example doesn't open a real socket; just pretend that there's
35 // already a TCP socket listening on port 8080. To experiment, you could turn on Web
36 // Sharing, then change this port number to 80 -- that will make your computer's local
37 // Apache web server reachable from the outside world while this program is running.
38 _mapper = [[MYPortMapper alloc] initWithLocalPort: 80];
40 // Optionally, request a public port number.
41 // The NAT is free to ignore this and return a random number instead.
42 _mapper.desiredPublicPort = 22222;
44 // Now open the mapping (asynchronously):
45 if( [_mapper start] ) {
46 Log(@"Opening port mapping...");
47 // Now listen for notifications to find out when the mapping opens, fails, or changes:
48 [[NSNotificationCenter defaultCenter] addObserver: self
49 selector: @selector(portMappingChanged:)
50 name: MYPortMapperChangedNotification
53 // MYPortMapper failed -- this is unlikely, but be graceful:
54 Log(@"!! Error: MYPortMapper wouldn't start: %i",_mapper.error);
63 - (void) portMappingChanged: (NSNotification*)n
65 // This is where we get notified that the mapping was created, or that no mapping exists,
66 // or that mapping failed.
68 Log(@"!! MYPortMapper error %i", _mapper.error);
70 NSString *message = @"";
71 if( !_mapper.isMapped )
72 message = @" (no address translation!)";
73 Log(@"** Public address:port is %@%@", _mapper.publicAddress, message);
74 Log(@" local address:port is %@", _mapper.localAddress);
91 TestCase(MYPortMapper) {
94 EnableLogTo(PortMapper,YES);
96 // Here's how to simply obtain your local and public address(es):
97 IPAddress *addr = [IPAddress localAddress];
98 Log(@"** Local address is %@%@ ...getting public addr...",
99 addr, (addr.isPrivate ?@" (private)" :@""));
100 addr = [MYPortMapper findPublicAddress];
101 Log(@"** Public address is %@", addr);
103 // Start up the test class to create a mapping:
104 MYPortMapperTest *test = [[MYPortMapperTest alloc] init];
106 // Now let the runloop run forever...
107 Log(@"Running the runloop forever...");
108 [[NSRunLoop currentRunLoop] run];