Archive for NSStream

iPhone Client for Multiplayer with NSStreams – Part 2

Posted in Uncategorized with tags , , , on March 7, 2010 by bersaelor

In the first version of the communicator class we were only handling messages that were smaller then 1024 Byte.

To allow it to receive larger messages we need to know when an individual message is finished. Therefore we have to add a unique specifier to the string our server sends, for example “\r\n”. Then we can simply add an “NSMutableString msgBuffer” to our class and another method between the “- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode;” and the “- (void) handleInput: (NSString*)string;” method that does the following:

- (void) processString:(NSString*)string {  
     [msgBuffer appendString:string];
     [string release];
     NSRange r=[msgBuffer rangeOfString:@"\r\n"];
     if (r.location!=NSNotFound) {
          [self handleInput:[[NSString alloc] initWithString:[msgBuffer substringToIndex:r.location]]];
          [msgBuffer setString:@""];
    }
}

Objective C professionals probably realize that I don’t like “autorelease”. The string-argument is supposed to be retained, as is the argument to “[self handleInput]”. I know an object is supposed to be released where it was retained but my original string was retained in “handleEvent” and released in “handleInput” and I didn’t want to change those two methods. This code would not be consistent with the older post otherwise.

Advertisements

iPhone Client for Multiplayer with NSStreams

Posted in Uncategorized with tags , , , , , on March 2, 2010 by bersaelor

When I was learning how to show Models with OpenGL I had the great Tutorials by Jeff Lamarche to help me with. But now that I was facing the development of a client-server pattern to build a multiplayer experience for my game I was stuck, because I couldn’t find any good help here.

Maybe there are others out there who’d appreciate some clarification and sample codes about that, so I’m trying to post something that I would have liked to use, when I was starting with the Topic.

Continue reading