odd behavior while transmit byte[] -> String -> Socket -> String -> byte[]

Hello friends!

I develop application for WPAN controlling. Everything works ok except one moment.

I have frame (byte array) which include IP address, type of operation, id parameter. Then I send this frame through the sockets. So all frames are sending and receiving properly except one.

Here is simple example of my problem (pay please your attention on figure 10 at the end of frame)

in servelet

byte[] frame = {100, 100, 100, 100, 1, 10};

outWpan = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);

strFrame = new String (frame);

// send to the WPAN server from servlet

outWpan.println(strFrame);

in socket server

--

byte[] frame;

while (true) {

String str = in.readLine();

if(str.equals("END"))

break;

// get bytes from servlet client

frame = str.getBytes();

System.out.println("the lenght of frame " + frame.length + "\n");

for(int i=0; i<frame.length; i++)

System.out.println(frame + "\n");

}

So if the id (last digit in the frame) is 10 - it doesn&#8217;t transmit this '10' to the destination socket and after this operation frame recieve {100, 100, 100, 100, 1} frame i.e. length is 5 instead 6 and it loses this last figure. In others cases everything is ok - for example if I change in my HTML page id operation on arbitrary figure it works.

I will be thankful for any guess.

Thank you.

>

[1534 byte] By [fet_frumos] at [2007-9-30 5:21:01]
# 1
The ASCII code 10 is the end of line (EOL) character. The readLine() call removed the EOL character.You shouldn't use readers and writers to read and write binary data. Readers and writers are for character data. Use InputStreams and OutputStreams for binary data.
jesperdj at 2007-7-1 17:06:26 > top of Java-index,Administration Tools,Sun Connection...
# 2

> You shouldn't use readers and writers to read and write binary data. Readers and writers are for

> character data. Use InputStreams and OutputStreams for binary data.

They're all InputStreams and OutputStreams anyway. To be a little more specific, use DataOutputStream / DataInputStream for writing arbitrary serialized data into a stream and reading it back.

Oh, and don't forget to flush() your DataOutputStream after sending each packet. As a further efficiency step, you may want to first wrap your Socket{In,Out}putStream with a Buffered{In,Our}putStream, and then wrap *that* with a Data{In,Out}putStream.

Then, whenever you write a packet, make sure you flush() the DataOutputStream() to make sure that all of your data gets written out to the socket.

shankar.unni at 2007-7-1 17:06:26 > top of Java-index,Administration Tools,Sun Connection...