Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to detect completion of Socket recv() function.

How to detect completion of Socket recv() function.

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorial
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Robert Palma Jr
    wrote on last edited by
    #1

    Vis C/C++ 6.0 Vis Studio I am receiving web pages using recv(). I am looping and receiving chunks of the page and am concatinating them to a buffer. I get the whole file but the loop hangs because I have no detection of the end of file. If I manually close the socket, I always have the full file. I have tried the recv() MSG_PEEK flag inside of the loop to detect the end of the file, but I don't seem to be able to get it to work. It hangs as well. I've seen some code on CodeProject that looks for 2 consequtive CRLFs but that really only works if you are only expecting a Header. Any help would be much appreciated. Thanks, Robert :-)

    Z A 2 Replies Last reply
    0
    • R Robert Palma Jr

      Vis C/C++ 6.0 Vis Studio I am receiving web pages using recv(). I am looping and receiving chunks of the page and am concatinating them to a buffer. I get the whole file but the loop hangs because I have no detection of the end of file. If I manually close the socket, I always have the full file. I have tried the recv() MSG_PEEK flag inside of the loop to detect the end of the file, but I don't seem to be able to get it to work. It hangs as well. I've seen some code on CodeProject that looks for 2 consequtive CRLFs but that really only works if you are only expecting a Header. Any help would be much appreciated. Thanks, Robert :-)

      Z Offline
      Z Offline
      zzattack
      wrote on last edited by
      #2

      I have no experience at all, but did you try using http / 1.0 or add the "Connection: close" header?

      R 2 Replies Last reply
      0
      • R Robert Palma Jr

        Vis C/C++ 6.0 Vis Studio I am receiving web pages using recv(). I am looping and receiving chunks of the page and am concatinating them to a buffer. I get the whole file but the loop hangs because I have no detection of the end of file. If I manually close the socket, I always have the full file. I have tried the recv() MSG_PEEK flag inside of the loop to detect the end of the file, but I don't seem to be able to get it to work. It hangs as well. I've seen some code on CodeProject that looks for 2 consequtive CRLFs but that really only works if you are only expecting a Header. Any help would be much appreciated. Thanks, Robert :-)

        A Offline
        A Offline
        Ajesh Sreevalsa
        wrote on last edited by
        #3

        Receive returns the number of bytes now in buffer(Received). If it is 0(zero) Then received all.You can turn back.

        R 1 Reply Last reply
        0
        • A Ajesh Sreevalsa

          Receive returns the number of bytes now in buffer(Received). If it is 0(zero) Then received all.You can turn back.

          R Offline
          R Offline
          Robert Palma Jr
          wrote on last edited by
          #4

          Hi Ajesh, Thank-you for reply. Unfortunately what is happening is that the recv() call is blocking - it is hung, so I don't get any "number of bytes" to even look at! If I manually close the socket, then I have the full file, but a 'manual close' approach would be very undesirable to implement. Any other suggestions? Many thanks, Robert

          1 Reply Last reply
          0
          • Z zzattack

            I have no experience at all, but did you try using http / 1.0 or add the "Connection: close" header?

            R Offline
            R Offline
            Robert Palma Jr
            wrote on last edited by
            #5

            Hi zzattack, Thanks for reply. I am using the recv() function and it is a blocking call. Since I do not know the size of the file that is coming, I keep calling recv() to get the full file. What happens is that when I call it the final time, the call blocks - and I am hung there. Robert

            1 Reply Last reply
            0
            • Z zzattack

              I have no experience at all, but did you try using http / 1.0 or add the "Connection: close" header?

              R Offline
              R Offline
              Robert Palma Jr
              wrote on last edited by
              #6

              Hi zzatack, Got it working! :-) I added Connection: close as you recommended. I guess that the recv() function was continuing to wait for more packets since the connection was still open. I'm not sure how you can say, "I have no experience at all..." when in fact you put your finger right on the problem!! Thanks again Robert :-)

              Z 1 Reply Last reply
              0
              • R Robert Palma Jr

                Hi zzatack, Got it working! :-) I added Connection: close as you recommended. I guess that the recv() function was continuing to wait for more packets since the connection was still open. I'm not sure how you can say, "I have no experience at all..." when in fact you put your finger right on the problem!! Thanks again Robert :-)

                Z Offline
                Z Offline
                zzattack
                wrote on last edited by
                #7

                Haha, cool, good to hear. I'll try to explain how I think it works. Actually your recv() function returns when it has read what you wanted (like 4096 or 8192 bytes etc.), or if the connection closes. But, if there's not enough data left to fill the buffer (eof), recv() keeps waiting, and the connection remains opened so that more data can be sent and added to the buffer later. By default on HTTP/1.0 the connection is closed, but with HTTP/1.1 it remains opened unless "Connection: close" was sent along with the headers. Therefor, recv() returns only when the server closes the connection because of a timeout. That might take minutes, and your code inded appears to hang :( I meant I have no c/c++ experience, only a bit of the HTTP protocol which I learned fom mIRC scripting. I was actually looking at the boards for tutorials :). Anyway, have fun.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups