NSNetService Won't Allow Subscription
NSNetService Won't Allow Subscription
- Subject: NSNetService Won't Allow Subscription
- From: "Jordan Evans" <email@hidden>
- Date: Mon, 24 Jul 2006 10:15:55 -0700
I'm setting up a class that has a NetService called
"_connectiontype._tcp." A main function of this class is meant to
publish it's connection service "_connectiontype._tcp." , browse for
other types like it, then connect to all services of it's type. So,
when two or more of these services exists, they will all connect to
each other automatically upon finding each other. That is the gist of
what the function is.
I created two apps with the identical code, but each named it's
connection service different of course. So far I have got each
service of both apps to publish and to find each other using
NSNetServiceBrowser. But, only first application is able to subscribe
to the second apps connection service. When I try to get the second
app to connect to the first apps connection service, it just won't
connect for some reason.
I used the following code in two different applications, except I
changed the name of the service for the second app of course.
Here is my code, where am I going wrong?
//
// NetEntityManager.h
// ConnectionApp
//
// Created by Jordan Evans on 7/24/06.
// Copyright 2006 __MyCompanyName__. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface NetEntityManager : NSObject
{
NSNetService *connectionService;
NSNetServiceBrowser *connectionBrowser;
NSFileHandle *listeningSocket;
NSMutableArray *subscriptionSockets;
NSMutableArray *subscriberSockets;
NSNotificationCenter *nc;
NSMutableArray *connectionServices;
NSMutableArray *subscriptions;
}
- (void)vendService;
- (void)subscriptionNotification:(NSNotification *)notification;
- (void)setUpConnectionBrowser;
- (void)connectToAllConnectionServices;
@end
//
// NetEntityManager.m
// ConnectionApp
//
// Created by Jordan Evans on 7/24/06.
// Copyright 2006 __MyCompanyName__. All rights reserved.
//
#import "NetEntityManager.h"
#import <sys/socket.h>
#import <netinet/in.h>
@implementation NetEntityManager
- (id)init
{
self = [super init];
if ( self )
{
nc = [NSNotificationCenter defaultCenter];
connectionServices = [[NSMutableArray alloc] init];
subscriptions = [[NSMutableArray alloc] init];
subscriptionSockets = [[NSMutableArray alloc] init];
subscriberSockets = [[NSMutableArray alloc] init];
}
return self;
}
- (void)vendService
{
struct sockaddr_in addr;
int sockfd;
sockfd = socket( AF_INET, SOCK_STREAM, 0 );
bzero( &addr, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl( INADDR_ANY );
addr.sin_port = htons( 0 );
bind( sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr));
listen( sockfd, 5 );
int namelen = sizeof(struct sockaddr_in);
getsockname( sockfd, (struct sockaddr *)&addr, &namelen );
listeningSocket = [[NSFileHandle alloc] initWithFileDescriptor:sockfd];
[nc addObserver:self selector:@selector(subscriptionNotification:)
name:NSFileHandleConnectionAcceptedNotification object:nil];
[listeningSocket acceptConnectionInBackgroundAndNotify];
// Here is where I changed the name of the second app.
connectionService = [[NSNetService alloc] initWithDomain:@""
type:@"_connectiontype._tcp." name:@"serviceOne" port:addr.sin_port];
[connectionService setDelegate:self];
[connectionService publish];
}
- (void)subscriptionNotification:(NSNotification *)notification
{
NSLog(@"Subscription notification.");
[subscriberSockets addObject:[[notification userInfo]
objectForKey:NSFileHandleNotificationFileHandleItem]];
[nc addObserver:self
selector:@selector(receiveMessage:)
name:NSFileHandleReadCompletionNotification
object:[subscriberSockets lastObject]];
[[subscriberSockets lastObject] readInBackgroundAndNotify];
}
- (void)setUpConnectionBrowser
{
connectionBrowser = [[NSNetServiceBrowser alloc] init];
[connectionBrowser setDelegate:self];
[connectionBrowser searchForServicesOfType:@"_connectiontype._tcp."
inDomain:@""];
}
- (void)subscribeTo:(NSNetService *)service
{
NSData *address = [[service addresses] objectAtIndex:0];
int s = socket( AF_INET, SOCK_STREAM, 0 );
connect( s, [address bytes], [address length] );
[subscriptionSockets addObject:[[[NSFileHandle alloc]
initWithFileDescriptor:s] autorelease]];
[[subscriptionSockets lastObject] autorelease];
[nc addObserver:self selector:@selector(receiveDataNotification:)
name:NSFileHandleReadCompletionNotification
object:[subscriptionSockets lastObject]];
[[subscriptionSockets lastObject] readInBackgroundAndNotify];
}
- (void)receiveDataNotification:(NSNotification *)notification
{
// Uncoded.
}
- (void)connectToAllConnectionServices
{
int i;
for( i=0; i<[connectionServices count]; i++ )
{
[self subscribeTo:[connectionServices objectAtIndex:i]];
}
}
@end
@implementation NetEntityManager (NSNetServiceBrowserDelegation)
- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser
didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
{
NSLog( @"Found service named %@.", [aNetService name] );
[connectionServices addObject:aNetService];
[aNetService setDelegate:self];
[aNetService resolve];
if ( !moreComing )
;
}
@end
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden