Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

new to Java; Help Needed in networking ; packet structure equivalent in java

 
jkc simha
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there,

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
struct packet{
uint8 type;
uint8 sourceID;
uint16 countdata;
uint8 data[500];
}
:
:
:
// in main function
struct packet *pkt;
uint8 datagram [504];

// 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




Thanks in advance
 
Carey Evans
Ranch Hand
Posts: 225
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic