C Socket Reading TOO MUCH Data










0















I am making a server that should be able to accept requests from multiple clients. To ensure I am reading large requests properly, I made the below code segment.



Requests come in the form <START_REQUEST>a long message<END_REQUEST>



read(fd, buffer, BUFFER_SIZE);
// Keep Reading If Entire Message Not Recieved
int buffer_len = strlen(buffer);
char *end_tag = &buffer[buffer_len-strlen("<END_REQUEST>")];
while(strcmp(end_tag, "<END_REQUEST>") != 0)

char *temp_buffer;
temp_buffer = malloc(BUFFER_SIZE);

valread = read(fd, temp_buffer, BUFFER_SIZE);
strcat(buffer, temp_buffer);

free(temp_buffer);

buffer_len = strlen(buffer);
end_tag = &buffer[buffer_len-strlen("<END_REQUEST>")];



However, sometimes (very often) the contents of buffer are something like:
<START_REQUEST>a long message<END_REQUEST>somegarbagedataheremaybefromanotherequest?
and thus the loop never terminates.



Why might this be happening?










share|improve this question






















  • read() reads binary data, meaning it doesn't end the buffer with to make it safe to use with strcat(). Also why are you allocating the buffer only to free it again at every freaking loop? Do it only once.

    – Havenard
    Nov 15 '18 at 22:00







  • 1





    @Havenard Or, better yet, just read the data into wherever you want it in the first place. There's no point in reading it somewhere other than where you want it just to have to copy it to where you wanted it in the first place.

    – David Schwartz
    Nov 15 '18 at 22:13











  • @DavidSchwartz Indeed, using an intermediate buffer makes no sense.

    – Havenard
    Nov 15 '18 at 22:18















0















I am making a server that should be able to accept requests from multiple clients. To ensure I am reading large requests properly, I made the below code segment.



Requests come in the form <START_REQUEST>a long message<END_REQUEST>



read(fd, buffer, BUFFER_SIZE);
// Keep Reading If Entire Message Not Recieved
int buffer_len = strlen(buffer);
char *end_tag = &buffer[buffer_len-strlen("<END_REQUEST>")];
while(strcmp(end_tag, "<END_REQUEST>") != 0)

char *temp_buffer;
temp_buffer = malloc(BUFFER_SIZE);

valread = read(fd, temp_buffer, BUFFER_SIZE);
strcat(buffer, temp_buffer);

free(temp_buffer);

buffer_len = strlen(buffer);
end_tag = &buffer[buffer_len-strlen("<END_REQUEST>")];



However, sometimes (very often) the contents of buffer are something like:
<START_REQUEST>a long message<END_REQUEST>somegarbagedataheremaybefromanotherequest?
and thus the loop never terminates.



Why might this be happening?










share|improve this question






















  • read() reads binary data, meaning it doesn't end the buffer with to make it safe to use with strcat(). Also why are you allocating the buffer only to free it again at every freaking loop? Do it only once.

    – Havenard
    Nov 15 '18 at 22:00







  • 1





    @Havenard Or, better yet, just read the data into wherever you want it in the first place. There's no point in reading it somewhere other than where you want it just to have to copy it to where you wanted it in the first place.

    – David Schwartz
    Nov 15 '18 at 22:13











  • @DavidSchwartz Indeed, using an intermediate buffer makes no sense.

    – Havenard
    Nov 15 '18 at 22:18













0












0








0








I am making a server that should be able to accept requests from multiple clients. To ensure I am reading large requests properly, I made the below code segment.



Requests come in the form <START_REQUEST>a long message<END_REQUEST>



read(fd, buffer, BUFFER_SIZE);
// Keep Reading If Entire Message Not Recieved
int buffer_len = strlen(buffer);
char *end_tag = &buffer[buffer_len-strlen("<END_REQUEST>")];
while(strcmp(end_tag, "<END_REQUEST>") != 0)

char *temp_buffer;
temp_buffer = malloc(BUFFER_SIZE);

valread = read(fd, temp_buffer, BUFFER_SIZE);
strcat(buffer, temp_buffer);

free(temp_buffer);

buffer_len = strlen(buffer);
end_tag = &buffer[buffer_len-strlen("<END_REQUEST>")];



However, sometimes (very often) the contents of buffer are something like:
<START_REQUEST>a long message<END_REQUEST>somegarbagedataheremaybefromanotherequest?
and thus the loop never terminates.



Why might this be happening?










share|improve this question














I am making a server that should be able to accept requests from multiple clients. To ensure I am reading large requests properly, I made the below code segment.



Requests come in the form <START_REQUEST>a long message<END_REQUEST>



read(fd, buffer, BUFFER_SIZE);
// Keep Reading If Entire Message Not Recieved
int buffer_len = strlen(buffer);
char *end_tag = &buffer[buffer_len-strlen("<END_REQUEST>")];
while(strcmp(end_tag, "<END_REQUEST>") != 0)

char *temp_buffer;
temp_buffer = malloc(BUFFER_SIZE);

valread = read(fd, temp_buffer, BUFFER_SIZE);
strcat(buffer, temp_buffer);

free(temp_buffer);

buffer_len = strlen(buffer);
end_tag = &buffer[buffer_len-strlen("<END_REQUEST>")];



However, sometimes (very often) the contents of buffer are something like:
<START_REQUEST>a long message<END_REQUEST>somegarbagedataheremaybefromanotherequest?
and thus the loop never terminates.



Why might this be happening?







c sockets






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 21:50









Steven DavisSteven Davis

61




61












  • read() reads binary data, meaning it doesn't end the buffer with to make it safe to use with strcat(). Also why are you allocating the buffer only to free it again at every freaking loop? Do it only once.

    – Havenard
    Nov 15 '18 at 22:00







  • 1





    @Havenard Or, better yet, just read the data into wherever you want it in the first place. There's no point in reading it somewhere other than where you want it just to have to copy it to where you wanted it in the first place.

    – David Schwartz
    Nov 15 '18 at 22:13











  • @DavidSchwartz Indeed, using an intermediate buffer makes no sense.

    – Havenard
    Nov 15 '18 at 22:18

















  • read() reads binary data, meaning it doesn't end the buffer with to make it safe to use with strcat(). Also why are you allocating the buffer only to free it again at every freaking loop? Do it only once.

    – Havenard
    Nov 15 '18 at 22:00







  • 1





    @Havenard Or, better yet, just read the data into wherever you want it in the first place. There's no point in reading it somewhere other than where you want it just to have to copy it to where you wanted it in the first place.

    – David Schwartz
    Nov 15 '18 at 22:13











  • @DavidSchwartz Indeed, using an intermediate buffer makes no sense.

    – Havenard
    Nov 15 '18 at 22:18
















read() reads binary data, meaning it doesn't end the buffer with to make it safe to use with strcat(). Also why are you allocating the buffer only to free it again at every freaking loop? Do it only once.

– Havenard
Nov 15 '18 at 22:00






read() reads binary data, meaning it doesn't end the buffer with to make it safe to use with strcat(). Also why are you allocating the buffer only to free it again at every freaking loop? Do it only once.

– Havenard
Nov 15 '18 at 22:00





1




1





@Havenard Or, better yet, just read the data into wherever you want it in the first place. There's no point in reading it somewhere other than where you want it just to have to copy it to where you wanted it in the first place.

– David Schwartz
Nov 15 '18 at 22:13





@Havenard Or, better yet, just read the data into wherever you want it in the first place. There's no point in reading it somewhere other than where you want it just to have to copy it to where you wanted it in the first place.

– David Schwartz
Nov 15 '18 at 22:13













@DavidSchwartz Indeed, using an intermediate buffer makes no sense.

– Havenard
Nov 15 '18 at 22:18





@DavidSchwartz Indeed, using an intermediate buffer makes no sense.

– Havenard
Nov 15 '18 at 22:18












1 Answer
1






active

oldest

votes


















2














How are you expecting strcat to know how many bytes to append onto the buffer?



valread = read(fd, temp_buffer, BUFFER_SIZE);
strcat(buffer, temp_buffer);


After the call to read, valread holds the number of bytes you read and it's the only thing that holds this information. However, you attempt to append data read onto the existing buffer without using this value -- so there is no possible way strcat could conceivably know how many bytes to append onto the buffer. It's no wonder you append junk that you read before.



Similar problem here:



read(fd, buffer, BUFFER_SIZE);
// Keep Reading If Entire Message Not Recieved
int buffer_len = strlen(buffer);


Here you ignore the return value of read, so you have no way to know how many bytes you read. How are you expecting strlen to figure out how many bytes read put into the buffer?






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328376%2fc-socket-reading-too-much-data%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    How are you expecting strcat to know how many bytes to append onto the buffer?



    valread = read(fd, temp_buffer, BUFFER_SIZE);
    strcat(buffer, temp_buffer);


    After the call to read, valread holds the number of bytes you read and it's the only thing that holds this information. However, you attempt to append data read onto the existing buffer without using this value -- so there is no possible way strcat could conceivably know how many bytes to append onto the buffer. It's no wonder you append junk that you read before.



    Similar problem here:



    read(fd, buffer, BUFFER_SIZE);
    // Keep Reading If Entire Message Not Recieved
    int buffer_len = strlen(buffer);


    Here you ignore the return value of read, so you have no way to know how many bytes you read. How are you expecting strlen to figure out how many bytes read put into the buffer?






    share|improve this answer





























      2














      How are you expecting strcat to know how many bytes to append onto the buffer?



      valread = read(fd, temp_buffer, BUFFER_SIZE);
      strcat(buffer, temp_buffer);


      After the call to read, valread holds the number of bytes you read and it's the only thing that holds this information. However, you attempt to append data read onto the existing buffer without using this value -- so there is no possible way strcat could conceivably know how many bytes to append onto the buffer. It's no wonder you append junk that you read before.



      Similar problem here:



      read(fd, buffer, BUFFER_SIZE);
      // Keep Reading If Entire Message Not Recieved
      int buffer_len = strlen(buffer);


      Here you ignore the return value of read, so you have no way to know how many bytes you read. How are you expecting strlen to figure out how many bytes read put into the buffer?






      share|improve this answer



























        2












        2








        2







        How are you expecting strcat to know how many bytes to append onto the buffer?



        valread = read(fd, temp_buffer, BUFFER_SIZE);
        strcat(buffer, temp_buffer);


        After the call to read, valread holds the number of bytes you read and it's the only thing that holds this information. However, you attempt to append data read onto the existing buffer without using this value -- so there is no possible way strcat could conceivably know how many bytes to append onto the buffer. It's no wonder you append junk that you read before.



        Similar problem here:



        read(fd, buffer, BUFFER_SIZE);
        // Keep Reading If Entire Message Not Recieved
        int buffer_len = strlen(buffer);


        Here you ignore the return value of read, so you have no way to know how many bytes you read. How are you expecting strlen to figure out how many bytes read put into the buffer?






        share|improve this answer















        How are you expecting strcat to know how many bytes to append onto the buffer?



        valread = read(fd, temp_buffer, BUFFER_SIZE);
        strcat(buffer, temp_buffer);


        After the call to read, valread holds the number of bytes you read and it's the only thing that holds this information. However, you attempt to append data read onto the existing buffer without using this value -- so there is no possible way strcat could conceivably know how many bytes to append onto the buffer. It's no wonder you append junk that you read before.



        Similar problem here:



        read(fd, buffer, BUFFER_SIZE);
        // Keep Reading If Entire Message Not Recieved
        int buffer_len = strlen(buffer);


        Here you ignore the return value of read, so you have no way to know how many bytes you read. How are you expecting strlen to figure out how many bytes read put into the buffer?







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 22:11

























        answered Nov 15 '18 at 21:54









        David SchwartzDavid Schwartz

        138k14144226




        138k14144226





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328376%2fc-socket-reading-too-much-data%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党