objective c - Java ByteBuffer -> [NSData bytes] to UInt16 -


good day all.

i'm writing client-server application based on sockets. server written java, client - objective-c on ios sdk 7.

my server writes data connected socket next code:

//socket client = new ...; dataoutputstream out = new dataoutputstream(client.getoutputstream()); // send package string message = "pings"; // package body byte bodysize = (byte) message.length(); java.nio.bytebuffer buffer = java.nio.bytebuffer.allocate(2 + 2 + 1 + bodysize); buffer.putshort((short) 16); // package id (2 bytes) buffer.putshort((short) 7); // package header (2 bytes, bitmask options) buffer.put(bodysize); // calculate & put body size in 5th byte (1 byte) buffer.put(message.getbytes()); // put message buffer (+5 bytes)  out.write(buffer.array()); // write stream out.close(); // close stream 

then i'm trying parse accepted nsdata in objective-c client. here's code:

- (uint16)packageidfrompackagehead:(nsdata *)data {     const void *bytes = [data bytes];     uint16 packageid;     memcpy(&packageid, bytes + 0, 2); // first 2 bytes, must short = 16     return packageid; } 

but, in "packageid" variable have value "4096". this? 16 decimal value?

next, i'm trying next value, "package header". wrote following code:

uint16 header; memcpy(&header, bytes + 2, 2); // skip first 2 bytes, copy next 2 bytes, must short = 7 

i got huge value, 1792. why? number means? tried several offsets, +2, +3 - nothing. can't correct value. tried use conversion as

header = cfswapint32bigtohost(header); 

or

header = cfswapint32hosttobig(header); 

this leads nothing - got 0 every time after conversion.

next, when tried 5th byte (it contains our message string length, remember). here's code:

uint8 bodysize; memcpy(&bodysize, bytes + 4, 1); // 5th byte 

i got value

'\x05' 

and correct.

i'm input nsdata * value correct. here can see memory dump screenshot xcode:

debug mode. input nsdata * memory dump

where problem? in server code? or in client reading code? please help, spent 2 days find solution , still couldn't it.

thanks hot licks!

correct client code is:

const void *bytes = [data bytes]; uint16 packageid; // 16-bit value memcpy(&packageid, bytes + 0, 2); // copy first 2 bytes packageid = cfswapint16bigtohost(packageid); // fix endianness 16-bit value return packageid; // got correct value 

or, , it's more convenient, modify java code on server with:

java.nio.bytebuffer buffer = java.nio.bytebuffer.allocate(2 + 2 + 1 + bodysize); buffer.order(java.nio.byteorder.little_endian); 

after modification initial client code work correctly without using cfswapint... functions.

many-many help!


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -