Thread: [Winsock] receive only 4 Bytes..

  1. #1
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55

    [Winsock] receive only 4 Bytes..

    Hi All!

    I'm Writing a Winsock Server App, and Found, that one recv call just gives me 4 Byte of the Data the Client sent.

    Thats really Confusing

    So i'd like to know how to get the Whole Packet Data with one recv call.

    Thanks

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>one recv call just gives me 4 Byte of the Data
    My best guess, and that's all it is as you have bothered posting any code, is that you're passing sizeof(mybuffer) to recv(), maybe like so:

    bytes = recv(sd, buf, sizeof(buf), 0);

    This will be a problem is buf is a pointer, rather than an array, as sizeof() will tell you the size of the pointer. buf may well be an array outside of your current function, but that isn't visible at the time sizeof() is used.

    >>how to get the Whole Packet Data with one recv call.
    You can't, ever, guarantee that. recv() will read as much data as there is available up to the size specified. If the network is flow, and the incoming packets are fragmented for whatever reason, your app will get the data in chunks. You are responsible for putting them back together into something meaningful, TCP/IP only guarantees getting the bytes from one end to the other in the correct order etc, it does not perform application level packet reconstruction.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55
    >> My best guess, and that's all it is as you have bothered posting any code, is that you're passing sizeof(mybuffer) to recv()...
    Hmm Yes your Right! - see:
    recv(sClient, buffer, sizeof(buffer), 0);

    Thx Hammer

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>You can't, ever, guarantee that.
    Does this hold true if the "packet" is 1 byte? Just curious

    >>how to get the Whole Packet Data with one recv call.
    You can cheat
    Code:
    int recv(SOCKET sock, char* buf, int packetSize, bool recvAll)
    {
         int bytesReceived = 0;
         int temp;
         while(bytesReceived < packetSize)
         {
              temp = recv(sock, &buf[bytesReceived], packetSize - bytesReceived, 0);
    
              if(temp == 0 || temp == SOCKET_ERROR || !recvAll)
                   return temp;
    
              bytesReceived += temp;
         }
    
         return bytesReceived;
    }
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>You can't, ever, guarantee that.
    >>Does this hold true if the "packet" is 1 byte? Just curious
    Well, on a blocking socket recv() will either return 1, 0 or -1.

    : 1 - you got your byte
    : 0 - the connection ended
    : -1 - error

    So, no, you can't guarantee it'll get you that one byte.

    >>You can cheat
    If you're receiving a char array that you intend to use as a string, just remember to make buf 1 bigger than you actually pass to that function. That way you have somewhere to put a \0.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Page File counter and Private Bytes Counter
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 01-31-2008, 03:17 AM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM