• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to unpack Network Packed Binary header through a SocketChannel

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic