Howdy all.
Spic and span: I publish an NSNetService on one device, and it's picked up on another. Both devices need to act as a client and a server, so that iPhone A can connect to iPhone B, and vice-versa, but in this scenario, lets say:
iPhone A is the "server," or the one whose NSNetService instance is being resolved.
iPhone B is the "client," or the one who resolves the NSNetService.
Now, my question is, what's the flow of communication? I've looked through all of Apple's bonjour examples for the Mac and for the iPhone, and here's what I know:
Step 1: Sockets. You need to create a socket for your applications to communicate "through." I did this like the following:
Step 2: File Handles. This is where I start getting confused. Should each application (since it needs to act as both client and server, depending on the scenario) have two file handles, one that is configured to send data once it has been connected to (NSFileHandleConnectionAcceptedNotification) and one that is configured to read data once it receives it (NSFileHandleReadCompletionNotification)?
Step 3: Catching All the Data. Here's where it gets even worse. I know that when you add the observers to the sockets for the above notifications, you have to point the selectors to methods, so lets say I have:
Those methods, once I test the code in the simulator, are each run once. I don't know which device (server vs. client) is running the code, or which File Handles are being passed to which methods.
Here's what does work in my program so far.
When the client connects to the server, the connectionReceived: method is sent to the observer of the socket. (My application delegate.) Then, in that connection received method, I do this:
That writes the data for that NSNumber object to the file handle, correct?
But then, things get even more confusing, when streams enter the picture! The input and output streams for the net service have their delegates pointed to my application delegate as well. (I'm trying to consolidate all my methods into one class right now, just until I understand it a bit better.)
So once the data is written to the above File Handle, it's "caught" or read in the following delegate method. (From NSStream)
So I guess my question is:
How much of this is necessary, and what's the flow of communication between two devices? Once you have a Net Service, what's the easiest way to open a two-way communication link to it? I can send data from one device using the file handle and pick it up in another using that stream's delegate method, but then, how do I send data back?
That NSStream delegate method was used when only a single, writable File Handle was provided by the server. Since there's also a readable one now, that responds to the fileHandleDataRead: method, do I still need to work with streams? Or, if it's that easy to get the input and output streams of an NSNetService instance, why do we need File Handles at all?
Thanks for making it through that, I've never done any networking programming before, but it's pretty exciting stuff.
Spic and span: I publish an NSNetService on one device, and it's picked up on another. Both devices need to act as a client and a server, so that iPhone A can connect to iPhone B, and vice-versa, but in this scenario, lets say:
iPhone A is the "server," or the one whose NSNetService instance is being resolved.
iPhone B is the "client," or the one who resolves the NSNetService.
Now, my question is, what's the flow of communication? I've looked through all of Apple's bonjour examples for the Mac and for the iPhone, and here's what I know:
Step 1: Sockets. You need to create a socket for your applications to communicate "through." I did this like the following:
Code:
NSSocketPort *mySocketPort = [[NSSocketPort alloc] initWithTCPPort:12345];
Step 2: File Handles. This is where I start getting confused. Should each application (since it needs to act as both client and server, depending on the scenario) have two file handles, one that is configured to send data once it has been connected to (NSFileHandleConnectionAcceptedNotification) and one that is configured to read data once it receives it (NSFileHandleReadCompletionNotification)?
Step 3: Catching All the Data. Here's where it gets even worse. I know that when you add the observers to the sockets for the above notifications, you have to point the selectors to methods, so lets say I have:
Code:
- (void)connectionReceived:(NSNotification *)aNotification;
- (void)fileHandleDataRead:(NSNotification *)aNotification;
Those methods, once I test the code in the simulator, are each run once. I don't know which device (server vs. client) is running the code, or which File Handles are being passed to which methods.
Here's what does work in my program so far.
When the client connects to the server, the connectionReceived: method is sent to the observer of the socket. (My application delegate.) Then, in that connection received method, I do this:
Code:
- (void)connectionReceived:(NSNotification *)aNotification {
NSLog(@"Connection received");
NSFileHandle * incomingFileHandle = [[aNotification userInfo] objectForKey:NSFileHandleNotificationFileHandleItem];
NSData * representationToSend = [[[NSNumber numberWithInt:13] stringValue] dataUsingEncoding:NSUnicodeStringEncoding];
[[aNotification object] acceptConnectionInBackgroundAndNotify];
[incomingFileHandle writeData:representationToSend];
NSLog(@"Writing number to file handle.");
[incomingFileHandle closeFile];
}
That writes the data for that NSNumber object to the file handle, correct?
But then, things get even more confusing, when streams enter the picture! The input and output streams for the net service have their delegates pointed to my application delegate as well. (I'm trying to consolidate all my methods into one class right now, just until I understand it a bit better.)
So once the data is written to the above File Handle, it's "caught" or read in the following delegate method. (From NSStream)
Code:
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)event;
So I guess my question is:
How much of this is necessary, and what's the flow of communication between two devices? Once you have a Net Service, what's the easiest way to open a two-way communication link to it? I can send data from one device using the file handle and pick it up in another using that stream's delegate method, but then, how do I send data back?
That NSStream delegate method was used when only a single, writable File Handle was provided by the server. Since there's also a readable one now, that responds to the fileHandleDataRead: method, do I still need to work with streams? Or, if it's that easy to get the input and output streams of an NSNetService instance, why do we need File Handles at all?
Thanks for making it through that, I've never done any networking programming before, but it's pretty exciting stuff.