views:

23

answers:

1

When we send a large amount of data to the client,its ReceiveAsync event is being called more than one time and in each time we get a few piece of the packet.
What shall we do to get C# Silverlight Tcp Packet in one piece and through one event? Thank you in advance.

+2  A: 

You can't. The very nature of TCP is that data gets broken up into packets. Keep receiving data until you've got the whole message (whatever that will be). Some options for this:

  • First send the size of the message before the message itself.
  • Close the connection when the message has been sent (so the client can basically read until the connection is closed)
  • Add a delimiter to indicate the end of the message

I generally dislike the final option, as it means "understanding" the message as you're reading it, which can be tricky - and may mean you need to add escape sequences etc if your delimiter can naturally occur within the message.

Jon Skeet
Thank you for advance,but as I know Tcp/IP protocol doing this work.Why I must do that? Why I must care about it on Application level?
Noro
@Noro: Because TCP/IP just provides you with a stream of data, effectively. It's up to the application protocol to decide how to handle that stream - how to divide it into messages etc.
Jon Skeet