my dog learned polymorphism*
The moose likes Sockets and Internet Protocols and the fly likes new to Java; Help Needed in networking ;  packet structure equivalent in java  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "new to Java; Help Needed in networking ;  packet structure equivalent in java  " Watch "new to Java; Help Needed in networking ;  packet structure equivalent in java  " New topic
Author

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

jkc simha
Greenhorn

Joined: Feb 26, 2008
Posts: 1
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

Joined: May 27, 2008
Posts: 225

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: new to Java; Help Needed in networking ; packet structure equivalent in java