A simple socket connection framework for two–way peer–to–peer communication.
The framework is intended to be used in conjunction with NSNetService
and NSNetServiceBrowser
to create communication between two peers.
Add LNSocketConnection.xcodeproj
to your project. In your target's settings, add LNSocketConnection.framework
to embedded binaries.
Import the framework's umbrella header:
#import <LNSocketConnection/LNSocketConnection.h>
- (void)netService:(NSNetService *)sender didAcceptConnectionWithInputStream:(NSInputStream *)inputStream outputStream:(NSOutputStream *)outputStream
{
_socketConnection = [[LNSocketConnection alloc] initWithInputStream:inputStream outputStream:outputStream queue:_socketQueue];
_socketConnection.delegate = self;
[_socketConnection open];
}
- (void)netServiceDidResolveAddress:(NSNetService *)sender
{
_socketConnection = [[LNSocketConnection alloc] initWithHostName:sender.hostName port:sender.port queue:_socketQueue];
_socketConnection.delegate = self;
[_socketConnection open];
}
[_socketConnection readDataWithCompletionHandler:^(NSData * _Nullable data, NSError * _Nullable error) {
if(error)
{
NSLog(@"Read error: %@", error);
return;
}
NSLog(@"Read data: %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}];
NSString* str = [NSString stringWithFormat:@"The time is: %@", [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle]];
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
[_socketConnection writeData:data completionHandler:^(NSError * _Nullable error) {
if(error)
{
NSLog(@"Write error: %@", error);
return;
}
NSLog(@"Successfully wrote data");
}];
Originally developed by me for Wix.