Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

connect() returns Refused Connection Error



I'm trying to get two applications to connect to each other's
NSNetService of the same type.  I'm trying to set it up so that when
AppA connects to AppB's service, AppB immediately connects to AppA's
service.  AppA connects fine, I get no connection error.  But, when
AppB try's to immediately connect to AppA's service, I get a
"connection refused" error.

1. Each App's service is published and watches in the background for
connections.

2. When a connection occurs, a -subscriptionNotification: is sent.

3. The -subscriptionNotification: implemetation is this:

- (void)subscriptionNotification:(NSNotification *)notification
{
	NSLog(@"Subscription notification.");
	
	// Retrieve the remote file handle.
	[subscriberSockets addObject:[[notification userInfo]
objectForKey:NSFileHandleNotificationFileHandleItem]];

	// Create structure for getting remote address.
	struct sockaddr addr;
	int namelen = sizeof(struct sockaddr);
	struct sockaddr *addressPtr;
	
	// Get the file descriptor of the remote socket.
	int fh = [[subscriberSockets lastObject] fileDescriptor];

	// Get the address of the remote peer.
	if(getpeername(fh, &addr, &namelen));
		perror( "getpeername" );
	// No error here.
		
	// Assign pointer to remote address, so pointer can be passed in
connect() function.
	addressPtr = &addr;
	// Debugger proves address is identical.

	// Create a socket for connecting to the remote socket.
	int s = socket( AF_INET, SOCK_STREAM, 0 );

	// Connect the remote address to the new socket.
	if( connect( s, addressPtr, namelen ) )
		perror( "connect" );

// Connection error here:     "connect: Connection refused"

	...
}

Any idea why I am refused a connection to the peer?
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.