News:

Facebook killed the radio star. And by radio star, I mean the premise of distributed forums around the internet. And that got got by Instagram/SnapChat. And that got got by TikTok. Where the fuck is the internet we once knew?

Main Menu

[Java] Incorrect Buffer Output

Started by Lance, July 11, 2008, 07:06:35 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Lance

I have a Java application that connects to a server.
Upon a successful connection, it sends data.

For some reason, values in the character buffer like 0x9c will be turned into 3f (as seen in wireshark) and the result will be a buffer with incorrect data. I am using PrintWriter.write so there is no encoding. Anyone know what's causing this or how to fix this? Thanks.
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!

Joe

I'm guessing you're seeing the TCP header. Thats first 0x36 bytes of the packet, IIRC. If you start reading from WireShark at offset 0x37, that might be it.
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


Lance

It's 324 bytes of data, and no, it's not the header :p
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!

warz

Quote from: SarCaSTiC on July 11, 2008, 07:06:35 PM
Anyone know what's causing this or how to fix this? Thanks.

Well, without any example code, I'm going to have to say you're either looking at the tcp header, as joe pointed out, or the obvious - your buffer is messed up. You could also be putting data into the buffer incorrectly. Sounds like you've got a coding error. grats.
http://www.chyea.org/ - web based markup debugger

Camel

#4
You can't use a Writer for binary data. Writers are to Streams as Strings are to byte arrays. You need to use byte arrays for binary data; if you Strings, you can be guaranteed that your code will work inconsistently in different environments.

Even if you use a Stream, Print* is still inappropriate for sockets (but not because it won't work). You should use a packet buffer to construct your packets and convert them to a byte[], and then write() that directly to the socket's OutputStream, thus making the write operation atomic, fault-resistant, and thread-safe.

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!

Lance

#5
This is a char[] array

Here it is:

char[] data = new char[] { 0x7d, 0x00 };
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!

Camel

Fine, don't take my word for it.

Also, a String is just a wrapper for a char[], like a Byte is a wrapper for a byte.

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!

Lance

#7
So this is going to be converted from hex to dec then put into a byte array and I'll be using a byte stream instead of Writer.
Sounds pretty easy :o

Sorry I completely misunderstood your first post

and Thanks :)
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!

MyndFyre

Quote from: SarCaSTiC on July 11, 2008, 08:27:16 PM
This is a char[] array

Here it is:

char[] data = new char[] { 0x7d, 0x00 };


Note that chars in Java are Unicode and always 16-bit values.
Quote from: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.

Camel

Quote from: MyndFyre on July 12, 2008, 02:30:48 AM
Note that chars in Java are Unicode and always 16-bit values.

That's not untrue, but is sort of misleading: Unicode *codepoints* are 32-bits, or ints in Java. This thinking was added to the Unicode standard after Java published String.class, so there are various encodings (almost always UTF-16) that can happen behind the scenes, and the encoding may change based on platform/environment. That's part of the reason for why Writers even exist in the first place; to abstract away the confusing encoding stuff.

In any event, I don't understand why you are using 16-bit values to store 8-bit numbers. Is that intentional? It seems like you should be using a byte[] instead of a char[] for your data.

<Camel> i said what what
<Blaze> in the butt
<Camel> you want to do it in my butt?
<Blaze> in my butt
<Camel> let's do it in the butt
<Blaze> Okay!