0

I have this Socket listener receiving data from the connected Socket clients. Each Socket client is stored inside a Client class with its own Buffer byte array and a few other properties.

I use an asynchronous receive method to receive the data, which then calls the callback method whenever data is received. So, my questions are...

Is there a possibility that data is received too quickly for the program to handle it efficiently?

And if so...

Is, locking the Buffer byte array, a good idea, in order to make sure the byte array is not being used before adding new data to it?

Hello World
  • 423
  • 5
  • 15
  • 1
    I've never had to do this, even with thousands of concurrent clients, so no... no locking is required. – spender Jan 16 '15 at 09:25
  • Ahh, I see. Thank you very much! This was very useful to know. I assume the Socket.BeginReceive() method handles that automatically, then. – Hello World Jan 16 '15 at 09:32
  • Is there a reason why you are bending your brain with the old APM methods rather than moving to the new async/await style? It really makes this sort of thing *a lot* easier. – spender Jan 16 '15 at 09:40
  • ....just make sure you don't BeginReceive before you've dealt with the contents of the buffer during the previous EndReceive. – spender Jan 16 '15 at 09:42
  • Well, I'm just more used to it and it's worked perfectly fine with me. Yeah, I always make sure I've completed handling the data before calling the BeginReceive() method again. Do you have any link you could recommend for me, for me to have a look at this "modern" async/await method? – Hello World Jan 16 '15 at 09:50
  • Here's an article by Stephen Toub about awaiting sockets... http://blogs.msdn.com/b/pfxteam/archive/2011/12/15/10248293.aspx If you don't mind using TcpListener instead, here's a bit of [demo code](http://stackoverflow.com/questions/12630827/using-net-4-5-async-feature-for-socket-programming/12631467#12631467) I wrote in answer to another question: – spender Jan 16 '15 at 09:58
  • Ahh, I had actually stumbled upon your demo code while having a look at the await-async socket method. Well, it definitely looks slightly cleaner, but as long as there is no big performance differences, I find the old APM method more convenient for me. Perhaps in the future, I'll give the new APM a shot. Thank you again for your clear information and help! Unfortunately you didn't make an "Answer", so I can't vote it as the answer. Regards – Hello World Jan 26 '15 at 18:27

0 Answers0