I am trying to add data received onto a buffer which needs to be configurable at runtime (I read a size from file or command line).
So basically I determine my buffersize and allocate an area of memory using calloc (I also put a catchall to set a buffersize if it is not in the config file or command line - Let's assume we use that for now).
I am only putting applicable lines of code.
int buffersize=10000;
void *BuffPtr = (void *)calloc(1,buffersize * sizeof(char));
I then have a recv from UDP (I have tried receiving into char array and dynamically allocated array - both work fine)
// Setup socket......
void *PktBuff = (void *)calloc(1,1000 * sizeof(char));
// Loop and receive many packets......
rcvd_bytes=recv(recv_socket, PktBuff, 1000, 0);
I can, at this point, write the contents of PktBuff and it works fine. But I want to concatenate a number of received packets in my dynamically allocated array (BuffPtr defined above).
I have tried strcat, but I just get garbage out if I try to write the first packet received, without getting another packet.
strcat(BuffPtr, PktBuff);
What I am doing wrong??
Thanks in advance.
calloccall wrong: The first argument is the number of items to allocate (buffersizein the first code chunk), and the second argument is the size of each items. It should becalloc(buffersize, sizeof(char)).