When I look at the buffer you receive, it indeed starts with 6005 (little Endian):
6005 is 0x1775 and the first two characters are u
(0x75) and Ctrl-W
(0x17). So you should get the int value by:
unsigned short primaryPort = (buffer[l] & 0xff) | ((buffer[l+1] & 0xff) << 8 );
and the same for the secondary port.
Edit
It works because:
In an expression like buffer[l] & 0xff
, buffer[i]
is implicitly casted into an int
(search for 'integer promotion' to learn more). Masking it with 0xff
is neccessary if buffer
is char *
: Then buffer[i]
is a signed char and thus, if the highest bit is set, it's value is negative (e.g. '\xff'
is -1). Masking it makes sure that we get a positive value in the range 0 - 255 instead. Last but not least, the shift operator << 8
shifts the byte value to the next higher byte the calculated int
. Finally the result of the complete operation is implicitly casted to unsigned short
which can be done safely because the calculated int
is less or equal to 0xffff.