well i'm no expert in C, but here is how I would do it:
#define HEADER_SIZE 13
struct RudpHeader
{
int seqNo;
int ackNo;
int advWin;
unsigned char ackFlag:1;
unsigned char finFlag:1;
};
int main()
{
struct RudpHeader header;
char* packet_data = calloc(1, HEADER_SIZE + 1024);
char* buffer = &packet_data[HEADER_SIZE]; //we can put data in here
header.seqNo = 100;
header.ackNo = 3542;
header.ackFlag = 1;
header.finFlag = 1;
//convert everything to network order before sending
header.seqNo = htonl(header.seqNo);
header.ackNo = htonl(header.ackNo);
header.advWin = htonl(header.advWin);
memcpy(&packet_data[0], &header.seqNo, 4);
memcpy(&packet_data[4], &header.ackNo, 4);
memcpy(&packet_data[8], &header.advWin, 4);
//no need to waste an int to send a bit, lets convert it
unsigned char flags = 0;
flags |= header.ackFlag;
flags |= header.finFlag << 1;
memcpy(&packet_data[12], &flags, 1);
//put some data in the buffer
char* msg = "Hello World!\0";
strcpy(buffer, msg);
//pretend you just received the packet and convert everything
//back to the hosts byte order
printf("Sequence Number: %d\nAcknowlegement Number: %d\nAdvWindow: %d\nFlags: %d\n",
ntohl(header.seqNo), ntohl(header.ackNo), ntohl(header.advWin), flags);
printf("Ack set: %d\nFin set %d\n", (flags&1), (flags&2));
printf("data: \"%s\"\n", buffer);
//now you can send the entire packet using variable 'packet_data'
//sendto(socket, packet_data, HEADER_SIZE + data_length, 0, sockaddr);
//either reuse the packet or destroy it(or just put it on the stack)
free(packet_data);
return 0;
}
note that I changed how the struct looks, a flag is not a integer so no need to waste data on the header. HEADER_SIZE == 4+4+4+1 which is 3 ints, 1 byte.
you should never ever send a struct across the network, and never assume the two machines have the same byte order. htonl converts a 32 bit number to network byte order and ntohl converts it to host order. htons and ntohs is the same thing, it just converts 16 bits instead. also when you receive a packet the buffer size will be (length of packet) - header. I hope I commented enough to make it obvious what is going on here.