wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Reading a stream of bytes from a socket. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading a stream of bytes from a socket." Watch "Reading a stream of bytes from a socket." New topic
Author

Reading a stream of bytes from a socket.

Susan Smith
Ranch Hand

Joined: Oct 13, 2007
Posts: 224
I'm reading a stream of bytes from a socket

Supposedly,
The first 4 bytes would give me either "2001"/ "2002"/ "2003"/ "2004"
The second 4 bytes would give me information about the number of bytes contained in the data portion of the message.
The 9th bytes to N would give me the data portion of the message.

I've tried to do these lines of codes below to try to get the first 8 bytes


Below is the System.out.print result for the first 8 bytes:



I don't understand what does this mean? I'm not sure whether I do something wrong or not.
Does this mean that there's something wrong with the stream.
Could someone please advice?

Thank you
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
First, the available() method is almost completely useless, and should be ignored here. It doesn't tell you how many bytes will eventually be available; it tells you how many are avaailable right now. Often you just need to wait for the rest of the data to be available. In general, if you want to read something until the end, check the return value of the read() method. If it returns -1, the stream is done. until then, just keep reading. See Available Doesn't Do What You Think It Does for more info.

It seems odd to read the data first with a DataInputStream to fill a byte[] array, then read that byte array with a ByteArrayInputStream and another DataInputStream. Seems like you're making more work for yourself there. I would probalby just use the first DataInputStream to do everything.

I don't see anything in your code which is taking the first 4 bytes, ro the first 8 bytes, and doing something special with them. I see code that takes the first 64 bytes, but I don't understand why you would want to do that.

If the first four bytes represent a number, it's important to know how the number was written. Was it written as text? (Probably not, based on the unprintable characters you're seeing.) Or was it written using some binary format? The most likely option is that it was written in the same format that writeInt() and readInt() use - methods on DateInputStream and DataOutputStream. See the API for more info. If that's the case then you should simply call readInt() twice to read the two numbers.

On further study, I don't think that's the case. Have you studied how binary and hexadecimal numbers work? You may need some special tricks to convert these four bytes into a single number. There are a number of ways to do this, but I don't know how much you know about manipulating bits and bytes. You can think of the four bytes as a number in base 256, where each byte is one digit. If the bytes were in order with the most significant byte first, you could just use readInt(). Instead, it looks to me like they're in the reverse order instead. You could try reordering the bytes before you use readInt(). Or you could use some other bit manipulation (e.g. shift operators) to push the bytes around and form a new int out of them. There's also a relatively simple way to read the numbers using a java.nio.ByteBuffer, if you're familiar with those - but if you're not, it may be better to use methods you are more familiar with. Does any of this sound familiar?
[ November 09, 2007: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
 
wood burning stoves
 
subject: Reading a stream of bytes from a socket.
 
Similar Threads
Reading Socket!!
Reading a stream of bytes from a socket.
Reading a stream from a socket
Reading Socket!!
Convert 2 bytes to a a Int