CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2008
    Posts
    9

    Unhappy what is correct behavior OnReceive?

    Hello, people,
    I have next problem:
    I need to sent a lot of small packets (50,100) bytes from server to client
    through asynchronous socket. First 10 btytes forms data header, and rest is data itself. There are possible few types of packets with deiffernt length and client is able to take real data length from HEADER.
    Naturally, on Client it was realized as 2 receives inside one OnReceive handler. In first, read HEADER, from second client gives data.
    It doesnot work correct, we have lost of data and dead loop with WSAEWOULDBLOCK.
    Looking like only one "receive" should be realized in ONReceive. It is not so good.. I am not able to take different lengths packets on one socket in this case!
    From your experience, is it reasonable? it means, am i able to use 2 "receives" in pre-described way and real problem is still outside solution or scheme with "one receive per OnReceive" is only valid and workable?
    thans a lot
    Hanzo

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: what is correct behavior OnReceive?

    1. Create a buffer (that can increase in size).
    2. On OnReceive read from the socket and append the data to the end of your buffer.
    3. Check the buffer if it contains a complete header, then determine if it contains a complete packet. If you got a complete packet, remove it from the buffer and do whatever necessary.
    4. Repeat step 3 until there are not more complete packets.
    (5. Wait for next OnReceive and jump to step 2.)

    - petter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured