title: how to decode C stuct like packets - with java socket programming
body: <p>new to socket programming in java!.   i am trying my level best to understand java socket programming, and how to decode 'C' strut like packets.</p>
  
  <p>The client is python which sends packet between tags,
   example Payload : <code>&lt;tag1&gt; data packet 1&lt;/tag1&gt;&lt;tag2&gt;data packet 2&lt;/tag2&gt;</code></p>
  
  <p>Have a c code to which received the payload and decode the payload with structure , all the tags are of 30 bytes, data packet 1, 2,.. n will be of different structure</p>
  
  <p>I am fairly new to java program, after searching through the web, i could write a socket server which receives payload from client, now facing problem in decoding the data packets.
  I could display the bytes. so Used Arrays.copyOfRange method to get data.</p>
  
  <p>The packet data are something represented as below,</p>
  
  <pre><code>typedef struct {
  
    unsigned char     dataType;
    unsigned char     value[4];
  
  } GEN_VAL
  
  typedef struct {
  
    unsigned char     dataType;
    unsigned char     numBytes;
    unsigned char     value[32];
  
  } GEN_STR_32;
  
  typedef struct {
  
      char           startTag[32];
      GEN_STR_32     c_code;
      GEN_STR_32     c_name;
      GEN_STR_32     c_info;
  
      GEN_VAL        i_type;
      GEN_VAL        i_count1;
      GEN_VAL        i_count2;
      char           endTag[32];
  
  } DATA_PACKET_1;
  </code></pre>
  
  <p>I receive buffer with following code snippet</p>
  
  <pre><code>  DataInputStream inStream = new DataInputStream
                          (new BufferedInputStream(socket.getInputStream()));
  
   while ( ( noOfBytes = (int)inStream.read(recvBuff)) != -1)
  {
   cDecode mydecode = new cDecode();
   mydecode.decodePacket(noOfBytes, recvBuff);
  }
  </code></pre>
  
  <p>cDecode.java</p>
  
  <pre><code>public void decodePacket(int TotalBytes, byte[] BuffRecv) 
  {
      byte[] GetTag = new byte[32];
      byte[] GEN_STR_32 = new byte[32];
      byte[] GEN_INT_4  = new byte[4];
  
       GetTag = Arrays.copyOfRange(BuffRecv,0,31);
       String TagStr = new String(GetTag).trim();  //get start tag 
  
       int startloc = 31;
       int offset = startloc + 32;
       GEN_STR_32 = Arrays.copyOfRange(Buff,startloc,offset);
       String cCode = new String(GEN_STR_32).trim();  // get value of cCode
       System.out.println("Code   :" + cCode );
  
       startloc = offset;
       offset = startloc + 32;
       GEN_STR_32 = Arrays.copyOfRange(Buff,startloc,offset);
       String cName = new String(GEN_STR_32).trim(); //get value of cName
       System.out.println("Name:" + cName );
  
       startloc = offset;
       offset = startloc + 32;
       GEN_STR_32 = Arrays.copyOfRange(Buff,startloc,offset);
       String cInfo = new String(GEN_STR_32).trim(); //get value of cName
       System.out.println("Info:" + cInfo );
  
       startloc = offset;
       offset = offset + 4;
       ByteBuffer iTypeByteBuff = ByteBuffer.wrap(GEN_DATA_INT_4);
       int iType= iTypeByteBuff .getInt();
       System.out.println("type :" + iType);
  
       startloc = offset;
       offset = offset + 4;
       // likewise used ByteBuffer for remaining integer data 
       // for receiving end tag, offset is added with 32!
  }
  </code></pre>
  
  <p>first 3 data is string value    and<br>
  second 3 data have integer value</p>
  
  <p>String value is displayed correctly.<br>
  finding problem in converting byte to integer, not sure whether i am using correct value for startloc and offset  in Arrays.copyOfRange method.</p>
  
  <p>I tried this based on the information i got from net. </p>
  
  <p>I also read about having a separate class without methods for all data structures. but i cant find any complete example, as 'sizeof' is not available in java.</p>
  
  <p>can some one guide me the correct way to decode packets in this scenario.</p>