How to save recv () output?

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) { /*memory checks stripped out since they are irrelevent for this post*/ /*store data to the appropriate area in htmlbff*/ 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 ?

+6
source share
4 answers

Use memcpy and htmlbff offset by q :

 memcpy(htmlbff + q, databff, c); 

You can similarly recv directly in htmlbff :

 c = recv(sock, htmlbff + q, MAXDATASIZE, 0)); 

But still, to keep a separate buffer, and depending on your complete code, this can make everything more clear.

Make sure you add checks against BUFFERSIZE so that you don't copy the htmlbff borders. You mentioned that you removed the realloc processing, so maybe you are already handling this.

Your permanent names are a bit confusing when I would use BUFFERSIZE to buffer the data to indicate the size of each fragment, i.e. databff size.

+2
source

Use memcpy() to copy (add) the data to htmlbff , but you also need to make sure that you are not exceeding the size of htmlbff . Either stop receiving data when receiving BUFFERSIZE bytes, or use realloc() to extend htmlbff to contain more data.

For instance:

 char* htmlbff; size_t htmlbff_size = BUFFERSIZE; htmlbff = malloc(htmlbff_size); if (htmlbff) { while((c = recv(sock, databff, MAXDATASIZE, 0)) > 0) { if (c + q > htmlbff_size) { htmlbff_size *= 2; /* Arbitrary doubling of size. */ char* tmp = realloc(htmlbff, htmlbff_size); if (tmp) { htmlbff = tmp; } else { /* memory allocation failure. */ free(htmlbff); htmlbff = 0; break; } } memcpy(htmlbff + q, databff, c); q += c; } } 
+5
source

What I would do is recv() data directly in htmlbff unless you need to do more processing on it.

Make sure realloc() htmlbff i - q less than MAXDATASIZE , so there is always room for another recv() .

Then you call recv(sock, htmlbff + q, MAXDATASIZE, 0)

+2
source

You need to continue redistributing / expanding the buffer so that it matches all the data (if the data read from the socket exceeds MAXDATASIZE) = Thus, when recv reads the data into the database, your htmlbff can grow in memory and then a new read can be added into your general htmlbff.

q and c look like cursors to keep track of where you are and how far you have to go.

 memcpy(htmlbff+q, databff, c); //Do this in your whle loop to append the data 
+1
source

Source: https://habr.com/ru/post/923684/


All Articles