I currently have a method that sends data through Bonjour. The problem is that my data is limited to 1000 kB. I know that if I want to send larger data I need to break it in to packets.
But this raises a question of how do I prevent packets from being lost, and ensure all packets are received by the receiver.
I am not good with network and would like to ask you to help me change this simple method to enable larger data transfer.
- (BOOL)sendData:(NSData *)data error:(NSError **)error {
BOOL successful = NO;
if(self.outputStreamHasSpace) {
NSInteger len = [self.outputStream write:[data bytes] maxLength:[data length]];
if(-1 == len) {
// error occured
*error = [[NSError alloc]
initWithDomain:ServerErrorDomain
code:kServerNoSpaceOnOutputStream
userInfo:[[self.outputStream streamError] userInfo]];
} else if(0 == len) {
// stream has reached capacity
*error = [[NSError alloc]
initWithDomain:ServerErrorDomain
code:kServerOutputStreamReachedCapacity
userInfo:[[self.outputStream streamError] userInfo]];
} else {
successful = YES;
}
} else {
*error = [[NSError alloc] initWithDomain:ServerErrorDomain
code:kServerNoSpaceOnOutputStream
userInfo:nil];
}
return successful;
}
Thank you.