• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: connect() returns Refused Connection Error
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: connect() returns Refused Connection Error


  • Subject: Re: connect() returns Refused Connection Error
  • From: Agent M <email@hidden>
  • Date: Thu, 27 Jul 2006 13:09:30 -0400

There are lots of reasons a connection could be refused but in your code below, it appears that you may not be getting the peer address appropriately. The socket address system is actually uses a bizarre inheritance-style system for each protocol type. So, the sockaddr structure isn't particularly useful (you want sockaddr_in for IPv4).

In any case, you should get the address information from the -address method of NSSocketPort. If that's not practical, you should follow the technique on p. 109 in Stevens' UNPv1. Good luck!

On Jul 27, 2006, at 11:35 AM, Jordan Evans wrote:

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?

¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ AgentM email@hidden ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬

_______________________________________________
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


References: 
 >connect() returns Refused Connection Error (From: "Jordan Evans" <email@hidden>)

  • Prev by Date: Re: Show/Hide columns in NSTableView?
  • Next by Date: NSMatrix setOrigin issue?
  • Previous by thread: connect() returns Refused Connection Error
  • Next by thread: NSMatrix setOrigin issue?
  • Index(es):
    • Date
    • Thread