I have a class for an RDT header that contains information for implementing several reliable data transfer protocols. I need to attach this information (only 12 bytes) to my send buffer in order to pass it over the socket. I am trying to use memcpy for this, but for some reason it just leaves garbage in the buffer. Below is a line of code that does not work. (RDT_HDR_SIZE is defined as 12).
Defining the variables that are passed to this function.
char payload[] = "sample code sample code";
int payload_size = sizeof(payload) ;
int pktsize = payload_size + sizeof( RdtHeader )+1 ;
char * send_buf = new char[pktsize];
A function with memcpy that has problems.
unsigned int Sender::CreateSegment( char * buf,
char payload[], int payload_size, unsigned long seqnum ) {
RdtHeader * header = (RdtHeader *) buf;
header->ack = 0;
header->fin = 0;
header->ok = 0;
header->seq = seqnum;
header->win = 0;
header->syn = 0;
memcpy( buf+RDT_HDR_SIZE, payload, payload_size );
return (payload_size + RDT_HDR_SIZE + 1);
}
If I take out RDT_HDR_SIZE, the payload is assigned properly for buf, however it destroys all the fields of my header. Any idea how to make this work?
Thank,
Eric R.
EDIT:
Here is the code for my RdtHeader class - maybe it will be useful.
class RdtHeader{
public:
u_char protocol:2;
u_char syn:1;
u_char fin:1;
u_char ok:1;
u_char reserved:3;
u_char unused;
u_short win;
u_long seq;
u_long ack;
};