tags:

views:

368

answers:

1

Here's my scenario. I have an application written in C++ but not the complete source but the "meat" of it is there. I also have a compiled exe of this application. It communicates to a server somewhere here on our network. I am trying to replicate the C++ code in java, however it uses dwords and memory references, sizeof etc, all things that don't exist in java since it manages it's own memory. It builds this large complex message and then fires it over the network. So I am basically sniffing the traffic and inspecting the packet and trying to hardcode the data it's sending over to see if I can get a response from the server this way. However I can't seem to replicate the message perfectly. Some of it, such as the license code it sends is in "clear hex", that is, hex that translates into ascii, where-as some other portions of the data are not "clear hex" such as "aa" which does not translate into ascii (or at least a common character set?? if that makes any sense I'm not sure).

Ideally I'd like to not do it like this, but it's a stepping stone to see if can get the server to respond to me. One of the functions is for the application to register itself and that's the one I am trying to replicate.

Some of my assumptions above may be wrong, so I apologize in advance. Thanks for your time.

+2  A: 

In Java, all "character" data is encoded as Unicode (and not ASCII). So when you talk to something outside, you need to map the internal strings to the outside world. There are several ways to do it:

  1. Use a ByteArrayOutputStream. This is basically a growing buffer of bytes to which you can append. This allows you to build the message using bytes.

  2. Use getBytes(encoding) where encoding is the encoding the other side understands. In your case, that would be "ASCII" for the text parts.

In your case, you probably need both. Create a byte buffer and then append strings and bytes to it and then send the final result (getByteArray()) via the socket API.

Aaron Digulla
Hey thanks, this seemed to work well. Alternatively as I was waiting on an answer I was able to create a character array and you can represent characters by using 0x?? where ?? can be any hex value, thus I was able to use "aa" here and have it represent the value I was trying to replicate. However your answer is obviously more appropriate from a readable/programmatic approach. Thanks very much.
ish
In Java "char" != "byte" (unlike as in C). You could create a byte[] array but then, you'd have to take care of the current position, etc. A lot of hazzle.
Aaron Digulla