Jump to content
  • Advertisement
Sign in to follow this  

stringWithUTF8String not work.

This topic is 2647 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have server at the java and application client at iphone. I can send list of online user at application client. All right, but function stringWithUTF8String can read user name or return nil.

Server code for send message

int lenUsersName = NameUsers.length;
byte[][] names = new byte[lenUsersName][];
int len = 5;
for (int i = 0; i < lenUsersName; i++) {
byte[] name = NameUsers.getBytes();
len += name.length + 4;
names = name;
ioBuffer = IoBuffer.allocate(len, false);
ioBuffer.put((byte) serverTypeMessage.ordinal());
for (int i = 0; i < lenUsersName; i++) {

Client code

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {

switch (eventCode) {

case NSStreamEventHasBytesAvailable:


NSMutableData *data = [[NSMutableData alloc] init];

NSInputStream *inputStream = (NSInputStream *) stream;

while ([inputStream hasBytesAvailable]) {

uint8_t buf[1024];

NSInteger len = [inputStream read:buf maxLength:1024];

if (len > 0) {

[data appendBytes:(const void *) buf length:(NSUInteger) len];



ServerMessage *mess = [[ServerMessage alloc] initWithInputData:data];

[data release];

if (CallBackObject != nil && CallBackFunc != nil) {

[CallBackObject performSelector:CallBackFunc withObject:mess];


[mess autorelease];





- (void)PlayerListParse:(const char *)temp {

uintptr_t offset = 1;

int lenUsersName = OSReadBigInt32(temp, offset);

offset += 4;

self.NameUsers = [[[NSMutableArray alloc] init] autorelease];

for (int i = 0; i < lenUsersName; i++) {

int lenName = OSReadBigInt32(temp, offset);

offset += 4;

const char *temp1 = temp + offset;

NSString *name = nil;

do {

name = [[NSString stringWithUTF8String:temp1] retain];

} while (name == nil);

offset += lenName;

[NameUsers addObject:name];

[name release];



- (id)initWithInputData:(NSData *)data {

self = [super init];

if (self) {

MessageData = [data retain];

NSLog(@"ServerMessage initWithInputData data=%@ len=%d", data, [data length]);

const char *temp = (const char *) [data bytes];

serverTypeMessage = (enum ServerTypeMessage) temp[0];

switch (serverTypeMessage) {

case ListPlayersServerTypeMessage:

NSLog(@"ServerMessage ListPlayersServerTypeMessage");

[self PlayerListParse:temp];




return (self);


do.. while - this is little save the situation, but i don't think what that good idea.

Help to understand what is wrong?

p.s: sometimes a function returns wrong string, but message from server in bytes is identical.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!