1/18 In C network programming, say I have a variable frame_var of
type ethernet frame struct. Ignoring what uint is actually, let's
say the frame struct is "struct frame {uint preamble, uint dest,
uint source, char data[1500], uint crc};". Then in order to
put stuff into data, I'd do casting as follows:
tcp_data = (tcp_struct *)&frame_var->data
From this point, I can put fields into tcp_data->source,
tcp_data->etc. How would I achieve the same thing is Java
since I can't do crazy dereferencing/crazy casting? And is it
actually possible to do such low level (data link layer)
programming in Java?
\_ If I'm understanding what you're wanting to do, you may want to
look at the serializable interface, and the corresponding
writeObject, readObject methods. -mice
\_ StringWriter maybe? Or just write to the socket directly.
\_ You might be able to do some of this using reflection.
I don't think that you can manually set the tcp src/dest
addr on a pkt in java though.
\_ I'd create a class TCPData with instance vars matching the
tcp_struct, and then add a method TCPData::writeOnBuffer(char[])
to shove the data into the char*. If you want to be a bit more
efficient with memory, you could do it differently -- have
the TCPData constructor accept a char[] and have the getter/setter
methods do the offset arithmetic to put and retrieve values
from the char[] correctly, but I'd only do that if absolutely
necessary. |