R
R Cubed
I am currently in the process of transferring my C++ knowledge to C
for a socket programming class I am taking.
I need to send a packet using the sendto() function. This packet
contains a header and data.
I have declared the following basic struct to hold the header part of
the packet:
struct packet {
int seqNum;
int type;
int dataSize;
} header;
I have a character array called buf of size MAXPKTSIZE (defined to be
10000 bytes). I need to copy the entire header struct into this array,
and then copy an array called fileContents into this array. How can I
do this?
I have tried:
memcpy(buf,&header,12);
//size is 12 because struct contains 3 ints of 4 bytes each.
To debug, I try to fprintf the value of buf, but either nothing
prints, or I get garbage (large negative integers). How can I
accomplish this?
Also, once I have copied the struct into buf, how can I append the
packet data to buf?
Your help is greatly appreciated.
Thanks,
Ryan
for a socket programming class I am taking.
I need to send a packet using the sendto() function. This packet
contains a header and data.
I have declared the following basic struct to hold the header part of
the packet:
struct packet {
int seqNum;
int type;
int dataSize;
} header;
I have a character array called buf of size MAXPKTSIZE (defined to be
10000 bytes). I need to copy the entire header struct into this array,
and then copy an array called fileContents into this array. How can I
do this?
I have tried:
memcpy(buf,&header,12);
//size is 12 because struct contains 3 ints of 4 bytes each.
To debug, I try to fprintf the value of buf, but either nothing
prints, or I get garbage (large negative integers). How can I
accomplish this?
Also, once I have copied the struct into buf, how can I append the
packet data to buf?
Your help is greatly appreciated.
Thanks,
Ryan