• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do you read packets off a stream?

 
Prasant Jagannath
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an older C++ application that I am looking to connect over the network to a java viewer. But unfortunately I cannot seem to figure out how to read pre-specified packets off a stream, or convert them to values. This is simple in C, you just just create a struct like so:
struct foo{
int id;
float val;
char terminator;
}
and ship that sucker over the wire, because structs are layed out in memory in sequence.
How do I receive something like this over the wire on a java app? I might be able to parse the stream looking for the end character (though I have no idea what to make the termination character so that it doesnt interfere with data contents), or I can make it read a fixed length into a bytearray.
However, I run into endianess problems, and data conversion problems. How do I convert bytearray byte[0]-byte[3] into an int?
Im totally stuck.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at DataInputStream - it provides a wide choice of methods for doing this sort of thing.
 
Prasant Jagannath
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um, this isnt working. I dropped the termination character, since it doesnt work in a binary stream such as the one we have here. So I have a standard C struct like so:
struct foo{
char type;
long val;
float time;
}
I tried to read this in via a sequence of readByte, readLong, and then a readFloat, and I got garbage out of the stream. I did however read the entire packet into a byte buffer, and it seems to be all there, I just cant read it out of the packet into these primitive datatypes. I was thinking of a solution of reading the packet into a data buffer, and then opening yet ANOTHER datainputstream on that buffer to read the values with readLong, etc.
Is there no way to do this? what am I doing wrong?
They seem to come in over the wire fine. Im working on an x86 platform, and the machine at the other end is an x86 running windows2000.
 
Prasant Jagannath
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know how to read the data into a buffer, but how do I cast the data back to primitive types?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!