ThoMoNetworking by Thorsten Karrer and Moritz Wittenhagen is a dead simple Objective-C class to handle client/server communication on a local network via Bonjour, for both Mac and iPhone/iPad development. It handles clients finding servers on a LAN without needing to exchange IP addresses and ports and deals with dropped connections and uses asynchronous network calls with threading.
Setting up a server and sending out messages to connected clients is just a couple of lines:
-(void)setupServer {
// Assume myServer exists
myServer = [[ThoMoServerStub alloc] initWithProtocolIdentifier:@"helloThoMo"];
[myServer start];
}
// Send a string to all our clients
-(void)sendString:(NSString *)aString {
[myServer sendToAllClients:aString];
}
The client then starts listening and a delegate is defined to send a message to when requests are received:
-(void)setupClient {
// Assume myClient exists
myClient = [[ThoMoClientStub alloc] initWithProtocolIdentifier:@"helloThoMo"];
[myClient setDelegate:self];
[myClient start];
}
// This is our delegate method where we receive data from our server
-(void)client:(ThoMoClientStub *)theClient didReceiveData:(id)theData fromServer:(NSString *)aServerIdString; {
NSLog(@"%@ sent me a message: %@", aServerIdString, theData);
}
There are a few limitations, which are discussed on the developer’s site (via Under the Bridge).