Hmmm...I shall explain more...I'm trying to do a cocoa application that have two-way communication. My UI contain one textview which is use for display the available domains and another textview for displaying the sended text. There is 3 buttons, Search(search for domain), Connect(to connect on the selected domain) & Send( to send text message to the domain user). Now I'm stuck after I select the domain and it display this error " NSNetServicesErrorCode = -72003; NSNetServicesErrorDomain = 10; "
Below is my full code... Please help to see for any mistake, thanks!
@interface ClientController ()
@property (readwrite, retain) NSNetServiceBrowser *browser;
@property (readwrite, retain) NSMutableArray *services;
@property (readwrite, assign) BOOL isConnected;
@property (readwrite, retain) NSNetService *connectedService;
@property (readwrite, retain) MTMessageBroker *messageBroker;
-(void)setupService;
-(void)stopService;
@end
@implementation ClientController
@synthesize browser;
@synthesize listeningSocket;
@synthesize services;
@synthesize isConnected;
@synthesize connectedService;
@synthesize socket;
@synthesize messageBroker;
-(void)awakeFromNib {
[self setupService];
services = [NSMutableArray new];
self.browser = [[NSNetServiceBrowser new] autorelease];
self.browser.delegate = self;
self.isConnected = NO;
}
-(void)setupService
{
// Start listening socket
NSError *error;
self.listeningSocket = [[[AsyncSocket alloc]initWithDelegate:self] autorelease];
if ( ![self.listeningSocket acceptOnPort:0 error:&error] ) {
NSLog(@"Failed to create listening socket");
return;
}
// Advertise service with bonjour
NSString *serviceName = [NSString stringWithFormat:@"Ohayo!-"];
connectedService = [[NSNetService alloc] initWithDomain:@"" type:@"_cocoaforsci._tcp." name:serviceName port:self.listeningSocket.localPort];
connectedService.delegate = self;
[connectedService publish];
}
-(void)stopService {
self.listeningSocket = nil;
self.socket = nil;
self.messageBroker.delegate = nil;
self.messageBroker = nil;
[connectedService stop];
[connectedService release];
[super dealloc];
}
-(void)dealloc {
[self stopService];
self.browser = nil;
[services release];
[super dealloc];
}
-(IBAction)search:(id)sender {
[self.browser searchForServicesOfType:@"_cocoaforsci._tcp." inDomain:@""];
}
-(IBAction)connect:(id)sender {
//[self setupBrowser];
//self.browser = nil;
//self.browser = [[NSNetServiceBrowser new] autorelease];
//self.browser.delegate = self;
self.listeningSocket = nil;
NSLog(@"setup browser");
NSNetService *remoteService = servicesController.selectedObjects.lastObject;
remoteService.delegate = self;
[remoteService resolveWithTimeout:30];
}
-(IBAction)send:(id)sender {
NSData *data = [textView.string dataUsingEncoding:NSUTF8StringEncoding];
MTMessage *newMessage = [[[MTMessage alloc] init] autorelease];
newMessage.tag = 100;
newMessage.dataContent = data;
[self.messageBroker sendMessage:newMessage];
}
#pragma mark Net Service Delegate Methods
-(void)netService:(NSNetService *)aService didNotPublish:(NSDictionary *)dict {
//Notify the appcontroller
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center postNotificationName:@"serviceUp" object:self userInfo:nil];
NSLog(@"Failed to publish: %@", dict);
}
- (void)netServiceWillPublish:(NSNetService *)sender
{
NSLog( @"Publishing service %@", [sender name] );
}
- (void)netServiceDidStop:(NSNetService *)sender
{
NSLog( @"Stopping service %@", [sender name] );
}
-(void)netService:(NSNetService *)service didNotResolve:(NSDictionary *)errorDict {
//[self handleError:[errorDict objectForKey:NSNetServicesErrorCode]];
//[services removeObject:service];
NSLog(@"Could not resolve: %@", errorDict);
}
-(void)netServiceDidResolveAddress:(NSNetService *)service {
NSError *error;
self.connectedService = service;
self.socket = [[[AsyncSocket alloc] initWithDelegate:self] autorelease];
[self.socket connectToAddress:service.addresses.lastObject error:&error];
}
- (void)netServiceWillResolve:(NSNetService *)sender
{
NSLog( @"Attempting to resolve address for %@...", [sender name] );
}
//Notifies the delegate that a domain was discovered.
- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser
didFindDomain:(NSString *)domainString
moreComing:(BOOL)moreComing
{
NSLog( @"Found domain %@.", domainString );
}
//Notifies the delegate when a previously discovered domain becomes unavailable.
- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser
didRemoveDomain:(NSString *)domainString
moreComing:(BOOL)moreComing
{
NSLog( @"Removed domain %@.", domainString );
}
#pragma mark AsyncSocket Delegate Methods
-(void)onSocketDidDisconnect:(AsyncSocket *)sock {
if(sock == self.socket)
{
self.socket = nil;
self.messageBroker = nil;
}
NSLog(@"Socket disconnected");
}
-(BOOL)onSocketWillConnect:(AsyncSocket *)sock {
if ( self.socket == nil)
{
self.socket = sock;
return YES;
}
return NO;
}
-(void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port {
MTMessageBroker *newBroker = [[[MTMessageBroker alloc] initWithAsyncSocket:sock] autorelease];
[sock release];
newBroker.delegate = self;
self.messageBroker = newBroker;
self.isConnected = YES;
}
#pragma mark MTMessageBroker Delegate Methods
-(void)messageBroker:(MTMessageBroker *)server didReceiveMessage:(MTMessage *)message {
if ( message.tag == 100 ) {
textView.string = [[[NSString alloc] initWithData:message.dataContent encoding:NSUTF8StringEncoding] autorelease];
}
}
#pragma mark Net Service Browser Delegate Methods
-(void)netServiceBrowser:(NSNetServiceBrowser *)aBrowser
didFindService:(NSNetService *)aService
moreComing:(BOOL)more
{
[servicesController addObject:aService];
[aService setDelegate:self];
[aService resolve];
if(more == NO)
[text reloadData];
}
-(void)netServiceBrowser:(NSNetServiceBrowser *)aBrowser
didRemoveService:(NSNetService *)aService
moreComing:(BOOL)more
{
[servicesController removeObject:aService];
if ( aService == self.connectedService ) self.isConnected = NO;
if(!more == NO)
[text reloadData];
}
- (void)netServiceBrowserDidStopSearch:(NSNetServiceBrowser *)aNetServiceBrowser
{
if ( aNetServiceBrowser == browser ) {
[services removeAllObjects];
[text reloadData];
}
}
- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser
didNotSearch:(NSDictionary *)errorDict
{
NSLog(@"There was an error in searching. Error Dictionary follows...");
NSLog( [errorDict description] );
}
- (void)netServiceBrowserWillSearch:(NSNetServiceBrowser *)aNetServiceBrowser
{
if ( aNetServiceBrowser == browser )
NSLog(@"We're about to start searching for domains..." );
else
NSLog(@"We're about to start searching for services..." );
}
@end