Author Topic: [Java] Incorrect Buffer Output  (Read 3946 times)

0 Members and 1 Guest are viewing this topic.

Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
[Java] Incorrect Buffer Output
« on: July 11, 2008, 07:06:35 pm »
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!

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [Java] Incorrect Buffer Output
« Reply #1 on: July 11, 2008, 07:17:55 pm »
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.
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
Re: [Java] Incorrect Buffer Output
« Reply #2 on: July 11, 2008, 07:53:54 pm »
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!

Offline warz

  • Hero Member
  • *****
  • Posts: 1134
    • View Profile
    • chyea.org
Re: [Java] Incorrect Buffer Output
« Reply #3 on: July 11, 2008, 08:07:18 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

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: [Java] Incorrect Buffer Output
« Reply #4 on: July 11, 2008, 08:20:03 pm »
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.
« Last Edit: July 11, 2008, 08:22:00 pm by Camel »

<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!

Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
Re: [Java] Incorrect Buffer Output
« Reply #5 on: July 11, 2008, 08:27:16 pm »
This is a char[] array

Here it is:
Code: [Select]
char[] data = new char[] { 0x7d, 0x00 };
« Last Edit: June 22, 2010, 09:30:15 am by Lance »
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!

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: [Java] Incorrect Buffer Output
« Reply #6 on: July 11, 2008, 08:29:55 pm »
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!

Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
Re: [Java] Incorrect Buffer Output
« Reply #7 on: July 11, 2008, 08:48:46 pm »
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 :)
« Last Edit: June 22, 2010, 09:30:20 am by Lance »
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!

Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: [Java] Incorrect Buffer Output
« Reply #8 on: July 12, 2008, 02:30:48 am »
This is a char[] array

Here it is:
Code: [Select]
char[] data = new char[] { 0x7d, 0x00 };

Note that chars in Java are Unicode and always 16-bit values.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline Camel

  • Hero Member
  • *****
  • Posts: 1703
    • View Profile
    • BNU Bot
Re: [Java] Incorrect Buffer Output
« Reply #9 on: July 13, 2008, 06:01:31 pm »
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!