dimanche 1 février 2015

iphone socket stream buffer different on simulator and iPhone 6 plus?

I have this sample code taken from the example of this iPhone chat server.


In my case I need to send larger amounts of data, 100k-200k thus I changed the buffer size to something that can accommodate what I want.


On the iOS simulator (emulating a 6plus) everything works perfectly, as soon as I try to debug on my iPhone 6plus I get only the first 3-4k?? Is there a way to get over this and if so how do I concatenate all the incoming messages to one single message as this is an XML file I need to parse...



@interface ViewController ()

@end

@implementation ViewController

bool connectionError = false;

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//[self initNetworkCommunication];

messages = [[NSMutableArray alloc] init];

}


- (void)initNetworkCommunication {
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"192.168.1.1", 6035, &readStream, &writeStream);
inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;

[inputStream setDelegate:self];
[outputStream setDelegate:self];

[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

[inputStream open];
[outputStream open];

}


- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {

switch (streamEvent) {

case NSStreamEventOpenCompleted:
NSLog(@"Stream opened");
break;

case NSStreamEventHasBytesAvailable:

connectionError = false;
if (theStream == inputStream) {

uint8_t buffer[204800];
int len;

while ([inputStream hasBytesAvailable]) {
len = [inputStream read:buffer maxLength:sizeof(buffer)];
if (len > 0) {

NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSUTF8StringEncoding];

if (nil != output) {
//NSLog(@"server said: %@", output);
//NSLog(@"message received!");

[self messageReceived:output];
}
}
}
}
break;

case NSStreamEventErrorOccurred:
NSLog(@"Can not connect to the host!");
connectionError = true;
[self connectionLost];
break;

case NSStreamEventEndEncountered:
break;

default:
NSLog(@"Unknown event");
}

}

- (void) connectionLost {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message:@"Connection to the server lost!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];

[alert show];

}

- (void) messageReceived:(NSString *)message {

[messages addObject:message];
//NSLog(@"server said: %@", message);
//NSLog(@"message received!");

syncDevices *syncDev = [syncDevices new];
NSData *data = [message dataUsingEncoding:NSUTF8StringEncoding];

NSArray *devArr;

devArr = [syncDev parseDeviceXMLData:data];

// extra parsing blah blah...

}

- (IBAction)joinChat:(id)sender {

[self initNetworkCommunication];

[self sendSocketMessage: @"iam:" message: _inputNameField.text];
}


- (void) sendSocketMessage:(NSString*) sendCommand message:(NSString*) sendMessage
{
// do something...

if (connectionError == true) {
[self initNetworkCommunication];
}

NSString *response = [NSString stringWithFormat: @"%@%@", sendCommand, sendMessage];
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
NSLog(@"clint sent: %@", response);
}



@end



Aucun commentaire:

Enregistrer un commentaire