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. recv after select

recv after select

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
3 Posts 3 Posters 1 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.
  • H Offline
    H Offline
    hanlei0000000009
    wrote on last edited by
    #1

    timeval timeOut;
    timeOut.tv_sec = 0;
    timeOut.tv_usec = 3000*1000;

    fd_set fdread, fdExcept;
    FD_ZERO(&fdread);
    FD_ZERO(&fdExcept);
    FD_SET(sock,&fdread);
    FD_SET(sock,&fdExcept);

    int iRet = 0;
    iRet = select(0, &fdread, NULL, &fdExcept, &timeOut);

    if(FD_ISSET(sock,&fdread))
    {
    //can read
    int iRecv = recv(sock, strRecv, 512*1024, 0);// extern char strRecv[512*1024]
    /*
    If receive buffer has been received 1024 BYTE, recv() no problem. after recv(), iRecv equals 1024.
    If receive buffer has been received a lot data, 50*1024 for example, recv() has problem.
    strRecv has not got data what is from send() and iRecv not equals 50*1024.
    notes: If no select(), only use recv(), that no problem when recv() 50*1024 BYTE
    */
    }

    E M 2 Replies Last reply
    0
    • H hanlei0000000009

      timeval timeOut;
      timeOut.tv_sec = 0;
      timeOut.tv_usec = 3000*1000;

      fd_set fdread, fdExcept;
      FD_ZERO(&fdread);
      FD_ZERO(&fdExcept);
      FD_SET(sock,&fdread);
      FD_SET(sock,&fdExcept);

      int iRet = 0;
      iRet = select(0, &fdread, NULL, &fdExcept, &timeOut);

      if(FD_ISSET(sock,&fdread))
      {
      //can read
      int iRecv = recv(sock, strRecv, 512*1024, 0);// extern char strRecv[512*1024]
      /*
      If receive buffer has been received 1024 BYTE, recv() no problem. after recv(), iRecv equals 1024.
      If receive buffer has been received a lot data, 50*1024 for example, recv() has problem.
      strRecv has not got data what is from send() and iRecv not equals 50*1024.
      notes: If no select(), only use recv(), that no problem when recv() 50*1024 BYTE
      */
      }

      E Offline
      E Offline
      erwin1984
      wrote on last edited by
      #2

      use: while( .... ) { select( ... ); recv( ... ); } to receive a lot data. because of the large TCP data block will be splitted to small pieces during transfer, the recv()'s size may not equal to send()'s size at one time.

      Erwin Yuan

      1 Reply Last reply
      0
      • H hanlei0000000009

        timeval timeOut;
        timeOut.tv_sec = 0;
        timeOut.tv_usec = 3000*1000;

        fd_set fdread, fdExcept;
        FD_ZERO(&fdread);
        FD_ZERO(&fdExcept);
        FD_SET(sock,&fdread);
        FD_SET(sock,&fdExcept);

        int iRet = 0;
        iRet = select(0, &fdread, NULL, &fdExcept, &timeOut);

        if(FD_ISSET(sock,&fdread))
        {
        //can read
        int iRecv = recv(sock, strRecv, 512*1024, 0);// extern char strRecv[512*1024]
        /*
        If receive buffer has been received 1024 BYTE, recv() no problem. after recv(), iRecv equals 1024.
        If receive buffer has been received a lot data, 50*1024 for example, recv() has problem.
        strRecv has not got data what is from send() and iRecv not equals 50*1024.
        notes: If no select(), only use recv(), that no problem when recv() 50*1024 BYTE
        */
        }

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        As Erwin mentions, you can never assume you'll get all the bytes you request in a single call to recv(). You must keep calling recv() until you get the number of bytes you're expecting. Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        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