Click to See Complete Forum and Search --> : what is correct behavior OnReceive?


Hanzo
March 13th, 2008, 12:20 PM
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

wildfrog
March 13th, 2008, 01:01 PM
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