In C, I had working code, but I have no idea why it worked, so I started rewriting it so that I could understand what was going on.
So far so good! I rewrote and 90% sure that I understand everything that is happening now; the problem is that I donβt know how to save the piece of data received by recv ( databff ) into my previously allocated buffer ( htmlbff ).
Consider the following code (note that I split this into several bits, so it only includes the basics, for example, no memory reallocation or leakage protection, etc.):
#define BUFFERSIZE 4096 #define MAXDATASIZE 256 char *htmlbff, databff[MAXDATASIZE]; int c, i = BUFFERSIZE, q = 0; if(!(htmlbff = malloc(i))) { printf("\nError! Memory allocation failed!"); return 0x00; } while((c = recv(sock, databff, MAXDATASIZE, 0)) > 0) { q += c; }
So (if I do it right, and everything goes as it seems to me) c is the size of the current data block, and q is the total amount of data received so far ( q increases by c every time the cycle repeats). I'm currently using q to process memory (in case someone asks a question), but I believe that this will also make sense in solving this problem.
Anyway, the question I ask is about the second comment. How to save data from recv to htmlbff ?
source share