开发者

Java - Objective C Socket UTF8 Problem

I'm programming a little Client - Server program. The Server is in Java and the Client IOS. On the Server i开发者_Go百科 use java.net.ServerSocket and on the Client the LXSocket. Sending Data (Integer, Double, Float, UTF8 String) from Client to Server works perfect, but when i'm trying to send a UTF8 String back to the Client it kinda fails. I can receive the message, but not convert it to a NSString.

Java sending Code:

public void sendMessage(java.net.Socket socket) throws IOException {
    DataOutputStream stream = new DataOutputStream(socket.getOutputStream());
    int i = swabInt(64);
    stream.writeInt(i);
    String message = "Message to send as UTF8.";
    int len = swabInt(message.length());
    stream.writeInt(len);
    stream.write(nachricht.getBytes("UTF8"));
}

IOS receiving Code:

-(IBAction)createSocket {
LXSocket *socket = [[[LXSocket alloc] init] autorelease];
[socket connect:@"localhost" port:11111];

int i = [socket readInt];
int len = [socket readInt];
NSData *data = [socket readBytesWithLength:len];
NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

Receiving the Stream works, but when i'm trying to init the NSString it crashes. Any ideas?

EDIT

Transmission works fine, but i found out something strange. I tried out this code:

NSString *test = @"The Message";
    const char *c = [test UTF8String];`
NSString *string = [[NSString alloc] initWithUTF8String:c];
NSLog(@"Decoding: %s", string);

But my Console says:

2011-03-17 22:51:27.555 Sockets[8641:207] Decoding: ¿Ø‚

So is there any way to get a NSString from this char array encoded with UTF8??


Okay i figured it out... Plain stupidity...

NSLog(@"%s", theString);

instead of

NSLog(@"%@", theString);

Epic fail.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜