I have two classes: TcpPacket and IpPacket. The tcpPacket should be stored in the data field of the ip packet. How can I reliably convert the tcpPacket object to a byte array so that i can store it in the IpPacket's data field? And upon receiving the packet on the other side, how to reliably convert it back into a packet object?
public static final class TcpPacket {
int source;
int destination;
public Packet( int source, int destination ) {
this.source = source;
this.destination = destination;
}
public String toString() {
return "Source: " + source + ", Dest: " + destination;
}
}
public static final class IpPacket {
byte[] data;
IpPacket( byte[] data ){
this.data = data;
}
}
TcpPacket tcpPacket = new TcpPacket( <someint>, <someint> );
// the following doesn't work because tcppacket is not a byte array.
IpPacket ipPacket = new IpPacket( tcpPacket );
How is this usually done?
Thanks Maricruzz