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.
The client is python which sends packet between tags,
example Payload : <tag1> data packet 1</tag1><tag2>data packet 2</tag2>
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
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.
The packet data are something represented as below,
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;
I receive buffer with following code snippet
DataInputStream inStream = new DataInputStream
(new BufferedInputStream(socket.getInputStream()));
while ( ( noOfBytes = (int)inStream.read(recvBuff)) != -1)
{
cDecode mydecode = new cDecode();
mydecode.decodePacket(noOfBytes, recvBuff);
}
cDecode.java
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!
}
first 3 data is string value and
second 3 data have integer value
String value is displayed correctly.
finding problem in converting byte to integer, not sure whether i am using correct value for startloc and offset in Arrays.copyOfRange method.
I tried this based on the information i got from net.
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.
can some one guide me the correct way to decode packets in this scenario.