The following piece of code runs fine when parallelized to 4-5 threads, but starts to fail as the number of threads increase somewhere beyond 10 concurrent threads
int totalRecieved = 0;
int recieved;
StringBuilder contentSB = new StringBuilder(4000);
while ((recieved = socket.Receive(buffer, SocketFlags.None)) > 0)
{
contentSB.Append(Encoding.ASCII.GetString(buffer, 0, recieved));
totalRecieved += recieved;
}
The Recieve
method returns with zero bytes read, and if I continue calling the recieve method then I eventually get the exception 'An established connection was aborted by the software in your host machine'. So I'm assuming that the host actually sent data and then closed the connection, but for some reason I never received it.
I'm curious as to why this problem arises when there are a lot of threads. I'm thinking it must have something to do with the fact that each thread doesn't get as much execution time and therefore there are some idle time for the threads which causes this error. Just can't figure out why idle time would cause the socket not to receive any data.
Edit: Just to clarify. Each thread has its own personal socket reading different data.