Can anyone please point me toward a way to unpack network packed data?
My goal is to create the interface to to read a 32 Byte Header followed by a zipped payload of varying sizes...
The Header has a format:
char [16] timestamp
int [4] data type (1=heartbeat, 2=XML)
int [4] sequence number
int [4] compressed payload size
int [4] uncompressed payload size.
Please note I can see the timestamp, datatype, and sequence number fine and reliably.
The compressed and uncompressed fields appear to be Network packed data and I see binary data when I try to print the fields. I need the compressed/uncompressed size to know how much data to read for the payload and how big it should unzip too.
NOTE: in perl I have this working with no problem with "unpack(N*", but
Java is killing me. I am a newbie... Maybe I should have opened with that.
I use a socketchannel and I read the header into a byte buffer.
ByteBuffer header = ByteBuffer.allocate(32);
ByteBuffer payload;
SocketAddress address = null;
SocketChannel sockchan = null;
byte [] barray = new byte[4];
int loop = 5;
int cnt = 1;
int dt = 0; // data type
int sq = 0; //sequence number
int co = 0; //compressed size
int un = 0; //uncompressed size
int bytesread =0;
try{
address = new InetSocketAddress(host, port);
sockchan = SocketChannel.open();
sockchan.connect(address);
}
catch (IOException e){
e.printStackTrace();
}
/////
// Register the socket w/FAA.
/////
if( ! register(sockchan)){
System.out.println("Unable to register socket to FAA!");
}
while(cnt < loop){
header.clear();
try{
bytesread = sockchan.read(header);
System.out.println( "bytes read:" + bytesread );
}
catch(IOException e) {
e.printStackTrace();
}
header.flip();
int headcnt =0;
int barraycnt =0;
while(header.hasRemaining()){
if( headcnt < 16 ){
System.out.println("char"+(char) header.get());
}else{
/////
// fill barray with next 4 bytes.
/////
//System.out.println(">=16....next byte data cast to int"+(int) header.get() +"....");
barray[barraycnt] = header.get();
System.out.println(">=16....next byte data cast to int"+ barray[barraycnt]+"....");
System.out.println("INT:"+barraycnt +"-"+barray[barraycnt]);
barraycnt++;
}
if( barraycnt == 4){
/////
// process full barray
/////
System.out.println("got here");
if( headcnt < 20 ){
dt = ntohl( barray );
//dt = pushToString( barray );
System.out.println("got here dt:"+dt+".");
}else if( headcnt < 24 ){
sq = ntohl( barray );
//sq = pushToString( barray );
System.out.println("got here sq:"+sq+".");
}else if( headcnt < 28 ){
co = ntohl( barray );
//co = pushToString( barray );
System.out.println("got here co:"+co+".");
}else{
un = ntohl( barray );
//un = pushToString( barray );
System.out.println("got here un:"+un+".");
}
////////////////////////////////////////////clear the array.
for(int i=0;i<barray.length;i++)
barray[i] = 0;
barraycnt=0;
}
System.out.println("got here 3 -" + headcnt );
headcnt++;
System.out.println("dt" +dt);
System.out.println("sq" +sq);
System.out.println("co" +co);
System.out.println("un" +un);
}
if( dt != 2 || co < 1 ){
/////
// HEARTBEAT
/////
System.out.println("This is a keep alive packet!");
try{
java.lang.Thread.sleep(3000);
}
catch ( InterruptedException e ){
e.printStackTrace();
}
}else{
/////
// XML
/////
System.out.println("This is an XML PACKET fixen to read the rest of the data!");
payload = ByteBuffer.allocate( co );
try{
bytesread = sockchan.read(payload);
System.out.println( "------------------------------bytes read:" + bytesread );
}
catch (IOException e){
e.printStackTrace();
}
}
cnt++;
}
}
////////////////////////////////////////////
// network to host language conversion 32 bit
////////////////////////////////////////////
public static int ntohl(byte[] x){
int res = 0;
for(int i = 0; i < 4; i++){
res <<=8;
res|=(int)x[i];
System.out.println("ggggg"+x[i]+"ggggggg:"+res);
}
return res;
}