Sign in to follow this  

stringWithUTF8String not work.

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[i].getBytes();
len += name.length + 4;
names[i] = 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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this