|
-
January 6th, 2007, 11:47 AM
#1
Asynchronous socket receive
Hi all!
I'm doing a server app in C# and have a little problem/question:
I call BeginReceive() method specifying the callback method. In the callback method, I call EndReceive() like this:
Code:
private void OnReceive(IAsyncResult ar)
{
User user = (User) ar.AsyncState;
int datalen = user.userSocket.EndReceive(ar);
Packet datapacket = new Packet();
datapacket.FromBytes(user.data);
HandlePacket(datapacket);
}
Now I read some articles and found out that data won't always arrive in packets, since TCP is a streaming protocol.
So my question is: what is the best way to ensure that I receive the full packet before trying to parse them?...
My data packets look like this: 2byte signature, 2bytes defining message type, 2 bytes representing a short value, which tells us how many more bytes the packet has.
So I can easily count the packet size: 6 + (the value that short holds) bytes.
Please tell me if you need some more info.
Thanks in advance!
Using .NET 2.0 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|