views:

133

answers:

2

I am a newbie in socket programming. I am developing a server client application. And I am using Asynchronous tcp ip socket. But now I am facing a problem. In my client side I am receiving my data by a 2kb byte array by beginReceive method. Its working perfectly if data size below or equals to 2 kb, but problem occurring when data size exceeding 2kb range. Please give me some solution.

+1  A: 

This is perfectly normal - you shouldn't expect to get all the data in one call, whether you're using synchronous or asynchronous calls, and whether you have a lot of data or a little.

You should keep reading until the read call indicates that there's no more data - or until you've got everything you need. If your protocol needs more than one request/response on a connection, you should either length-prefix each message so that the other side knows how much to read, or have some sort of delimiter to indicate the end of a message. Length-prefixing is much easier when it's suitable, but it doesn't easily support streaming - you have to end up with length-prefixed "chunks" and a final chunk to indicate when you're done.

Jon Skeet
Thanks for reply. But I dont know how to handle huge data transmission by socket application. Can you give me some idea please ?
Jatin
@Jatin: Without any idea of what you're doing with the data afterwards, it's hard to know what to suggest. You could write the data to disk as you receive it, for example.
Jon Skeet
ok got it. Thanks for help.
Jatin
+1  A: 

I agree with Jon's answer, wrt the fact that you shouldn't expect all your data in one read.

Here are some blogs that have helped me with this problem in the past: Aviad Ezra has an excellent series on Asynchronous Sockets:

This blog is particularly useful if you decide to go the length-prefixed route, the Author uses a MemoryStream as his temporary storage between reads:

ParmesanCodice