Thread: Determining Data Size For Network Send/Rec :: Winsock

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Determining Data Size For Network Send/Rec :: Winsock

    Hi.

    I am studying networking programming from Network Programming For Microsoft Windows, Second Edition by Anthony Jones and Jim Ohlund. I am just about ready to start on a new product that will be my first real Winsock program. I want to design and implement a simple message program (chat) that work over the IP protocol and TCP protocol.

    I have the basic program core design in my mind. However, I am stuck with one problem and it is a problem that even Jones and Ohlund implied could be difficult to overcome for inexperience network programmers.

    How do you predetermine the size of the data you receive?

    Winsock has two basic functions for receiving data: revc (IPv4 or Winsock 1.1+) and WSARecv (IPv6 or Winsock 2). WSARecv is a powerful tool for *predetermined* data size. You can send multiple packages at one time, but the data size must be predetermined (i.e. 10k, 20k, and finally 30k). I will probably use revc since it is more flexible. Even with recv, however, you must give a specific size (byte) to read. Jones and Ohlund recommend sends the size of the data in the first four byte of the data stream. However, what if the user wants to send something that is larger than what a bour byte char variable can hold?

    Please post any possible solutions.

    Thanks,
    Kuphryn

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Then they have done a lot of typing or are going to wait around alot for it to be received (or sent as it would have got a WSAEWOULDBLOCK at the server).

    My app sends the size first. Receiving jpg's and data over 52k without problem.

    2,147,483,647+ bytes is a lot of data in one send() and a few days on a 56K modem.

    The more difficult task is handling WSAEWOULDBLOCK and split recv()
    (when all the data does not arrive in the same read or more than one data item arrives in the same read or both)
    Last edited by novacain; 05-07-2002 at 01:04 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Thanks.

    Can you give me an example?

    Let say I want to sent this message:

    "Please give an example of how you would receive this message."

    // send(socket, &message, sizeof(message), 0);

    Kuphryn

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    iSize=lstrlen(sBuffer);
    send(socket,(char*)&iSize ,sizeof(int) ,FLAGS );
    send(socket,(char*)sBuffer ,lstrlen(sBuffer) ,FLAGS );

    Roughly

    Find size recv
    Code:
    While(iUsed<iRecv)
    switch(iBuffer)
    case READSIZE:
      find expected size //sizeof(int)
      iBuffer++
    case READBUFFER
      create new storage to size
      if(Do not have ALL the data)
          copy what you have to storage
          increase iUsed
          increase iOffsetToStorage
      else//have all the data
         copy to storage
         increase iUsed
         iBuffer++
    case HAVEALLDATA
      Process
      iBuffer=0
      free storage
    Remember to use the iOffsetToStorage (this is the amount already in storage) and iUsed (the amount of the buffer processed) when copying from the incomming buffer to the storage buffer.
    And that iBuffer is a static (I use a struct for each socket)
    Last edited by novacain; 05-07-2002 at 08:52 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Thanks.

    Consider these other two techniques by Dire.Wolf:

    http://www.gamedev.net/community/for...topic_id=93771

    and by Niklas Lindquist

    http://www.codeproject.com/script/co...541#xx173653xx

    Kuphryn

  6. #6
    Unregistered
    Guest
    A full msg is

    Size+String

    As chat programs receive small amounts of data you need to ensure that if the recv() gets more than a whole msg in one read then you can handle it (and less than a whole msg). I only skimmed the others but do they handle the situation where the recv() gets

    Size+String+Size

    or worse

    Size+String+1/2Size

    ect

    (NOTE I edited the algy to reflect a better mem handling)

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Last one was me not logged in.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. WSASend & WSARecv :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-16-2002, 05:51 PM