new to Java; Help Needed in networking ; packet structure equivalent in java
posted 6 years ago
Below i have shown snippet of code from my "c server"
which i implemented on Linux using c language but now i
want to achieve same functionality in java. It would be
great help on your part if your can provide any suggestions
related to this.
// packet definition
// in main function
struct packet *pkt;
uint8 datagram ;
// type cast
pkt = (struct packet *) datagram;
// update the memory directly..
pkt->type = 1;
pkt->sourceID = 2;
pkt->countdata = 20;
pkt->data = // put 20 char here
The usual way to implement this in Java is to use an ObjectInputStream or ObjectOutputStream to read or write each field individually, which can be a bit of a change from your C example. An alternative technique would read the entire packet into a ByteBuffer, then read the fields with the methods get(), getShort() and so on.
The Javolution Struct class builds on the latter technique and looks even more like C.