• 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: Big picture relationships between NSConnection, NSInputStream, NSOutputStream etc
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Big picture relationships between NSConnection, NSInputStream, NSOutputStream etc


  • Subject: Re: Big picture relationships between NSConnection, NSInputStream, NSOutputStream etc
  • From: Ken Tozier <email@hidden>
  • Date: Thu, 4 Dec 2008 19:39:08 -0500

OK. After reading the docs on NSStream, here's what I came up with.

- (BOOL) openStreamsWithMode:(MySQLConnectMode) inMode
{
[NSStream getStreamsToHost: host port: port inputStream: &inStream outputStream: &outStream];

if ((inStream != nil) && (outStream != nil))
{
connectMode = inMode;

[inStream retain];
[outStream retain];


		[inStream setDelegate: self];
		[outStream setDelegate: self];

[inStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];
[outStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode: NSDefaultRunLoopMode];


[inStream open];
[outStream open];

NSLog(@"inputStream: %@, outputStream: %@", inStream, outStream);

return YES;
}
else
NSLog(@"Failed to open inputStream: %@, outputStream: %@", inStream, outStream);

return NO;
}


- (void) stream:(NSStream *) inSender
		handleEvent:(NSStreamEvent) inEvent
{
	NSLog(@"Entered: stream:handleEvent: with sender: %@", inSender);

	if (inSender == inStream)
	{
		switch (inEvent)
		{
			case	NSStreamEventHasBytesAvailable:
					NSLog(@"NSStreamEventHasBytesAvailable");
					[self readInputStreamData];
					break;

			case	NSStreamEventErrorOccurred:
					NSLog(@"NSStreamEventErrorOccurred: %@", [inStream streamError]);
					break;

			case	NSStreamEventEndEncountered:
					NSLog(@"NSStreamEventEndEncountered");
					[self processStreamData];
					break;
		}

	}
}

 I tried it out and got the following printout to the console

> inputStream: <NSCFInputStream: 0x13d170>, outputStream: <NSCFOutputStream: 0x13d1e0>
> connection: <MySQLConnection: 0x824800>
> Entered: stream:handleEvent: with sender: <NSCFInputStream: 0x13d170>
> NSStreamEventErrorOccurred: Error Domain=NSPOSIXErrorDomain Code=61 "Operation could not be completed. Connection refused"
> Entered: stream:handleEvent: with sender: <NSCFOutputStream: 0x13d1e0>


Which seems to make sense because MySQL requires a handshake and NSSocket doesn't know how to respond to that handshake. To perform the handshake, it seems I need to descend into BSD sockets.

Looking at the "Picture browser" example project shows how to set up a socket for listening, but not how to set one up to send the initial connect request to a remote process

int						fdForListening;
struct sockaddr_in		serverAddress;
socklen_t				namelen			= sizeof(serverAddress);

// In order to use NSFileHandle's acceptConnectionInBackgroundAndNotify
// method, we need to create a file descriptor that is itself a socket,
// bind that socket, and then set it up for listening. At this point,
// it's ready to be handed off to acceptConnectionInBackgroundAndNotify.

if((fdForListening = socket(AF_INET, SOCK_STREAM, 0)) > 0)
{
memset(&serverAddress, 0, sizeof(serverAddress));

serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = 0; // allows the kernel to choose the port for us.


if(bind(fdForListening, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) < 0)
{
close(fdForListening);
return;
}


// Find out what port number was chosen for us.
if(getsockname(fdForListening, (struct sockaddr *) &serverAddress, &namelen) < 0)
{
close(fdForListening);
return;
}


chosenPort = ntohs(serverAddress.sin_port);

if(listen(fdForListening, 1) == 0)
{
handshakeSocket = [[NSFileHandle alloc] initWithFileDescriptor: fdForListening closeOnDealloc: YES];
}
}


I gather from that, I need to set the following serverAddress fields differently but after looking at "in.h" none of the "INADDR_xxx" modes jump out at me
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = 0; // allows the kernel to choose the port for us.


Which should I use "IN_CLASSA_HOST," "IN_CLASSB_HOST," other?

How does one set up a file descriptor for an initial connect request to a remote process?

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Big picture relationships between NSConnection, NSInputStream, NSOutputStream etc
      • From: Andrew Farmer <email@hidden>
References: 
 >Big picture relationships between NSConnection, NSInputStream, NSOutputStream etc (From: Ken Tozier <email@hidden>)
 >Re: Big picture relationships between NSConnection, NSInputStream, NSOutputStream etc (From: John Pannell <email@hidden>)
 >Re: Big picture relationships between NSConnection, NSInputStream, NSOutputStream etc (From: Ken Tozier <email@hidden>)
 >Re: Big picture relationships between NSConnection, NSInputStream, NSOutputStream etc (From: "Michael Ash" <email@hidden>)
 >Re: Big picture relationships between NSConnection, NSInputStream, NSOutputStream etc (From: Ken Tozier <email@hidden>)
 >Re: Big picture relationships between NSConnection, NSInputStream, NSOutputStream etc (From: "Michael Ash" <email@hidden>)

  • Prev by Date: indexOfObjectIdenticalTo: problem with NSArrayController
  • Next by Date: Re: indexOfObjectIdenticalTo: problem with NSArrayController
  • Previous by thread: Re: Big picture relationships between NSConnection, NSInputStream, NSOutputStream etc
  • Next by thread: Re: Big picture relationships between NSConnection, NSInputStream, NSOutputStream etc
  • Index(es):
    • Date
    • Thread