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. WSARecv and recv

WSARecv and recv

Scheduled Pinned Locked Moved C / C++ / MFC
questionjson
1 Posts 1 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.
  • C Offline
    C Offline
    csrss
    wrote on last edited by
    #1

    Hi. Lets say, we got such 2 scenarios where we are receiving data over a socket: 1. Using WSARecv

    CHAR Buffer[1024];
    WSABUF wsaBuffer;
    DWORD dwBytesRecved = 0;
    wsaBuffer.buf = (char *)Buffer;
    wsaBuffer.len = sizeof(Buffer);
    OVERLAPPED overLap;
    ::RtlSecureZeroMemory(&overLap, sizeof(overLap));
    DWORD dwFlags = 0;
    ::WSARecv(sock, &wsaBuffer, 1, &dwBytesRecved, &dwFlags, &overLap, NULL);
    wsaBuffer.buf[dwBytesRecved] = '\0';
    cout << wsaBuffer.buf << ", Bytes: " << dwBytesRecved << endl;

    2. And this one, using recv

    CHAR Buffer[1024];
    int recved = ::recv(sock, Buffer, size, 0);
    Buffer[recved] = '\0';
    cout << Buffer << ", Bytes: " << recved << endl;

    In both cases, we are receiving only one buffer. (WSABUF allows to specify more then 1 recv buffer). And now, in case of recv, i can do something like this: 1. i got my recv buffer size, lets say 1024 bytes and doing recv. 2. Ok, i got some data and its length is 512 bytes and it has been copied to my recv buffer. But i know, that i should receive 1024 bytes of data. 3. So i am going to recv data in a loop until all 1024 bytes arrived. 4. Ok, i got some data again and it seems like it is next 512 bytes, but this time i am calling recv like this:

    int recved = ::recv(sock, Buffer + already_received_number_of_bytes, size, 0);

    So the rest of received data is appended to my recv buffer and in the end my recv buffer contains all data. The question is: how can i do this with WSARecv ? Thanks.

    011011010110000101100011011010000110100101101110 0110010101110011

    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