tags:

views:

1118

answers:

2

I have a service that is responsible for reading and writing from a server. The reading and writing is done in different threads.

The reader thread is active all the time whereas write thread only activates to send message and dies once message is written to server and response is received.

The service runs for 4 days and after that I am getting this error continuously, whenever the service tries to read from the port:

System.Net.Sockets.SocketException: An operation on a socket could not be performed because 
the system lacked sufficient buffer space or because a queue was full
+2  A: 

This can happen when you establish a large number of outgoing connections very quickly and run out of local ports. You can read about the "ephemeral port range" here.

Or, you may simply be running out of sockets. Make sure you close them after you are done using them.

twk
I have one socket that I read on everytime, and there is a keepalive mechanism implemented (sending keepalive message to server and processing response) if connection is lost socket is closed and new socket is opened
I realized that I was not calling close on socket only shutdown.hopefully this will resolve the issue.
A: 

I would also strongly suggest you look at your write thread, and how you are referencing resources related to the Socket. Ie, are you calling Close/Dispose for any NetworkStream or other objects you are using with the Socket? A code sample from the write thread would be helpful if you're still struggling with the problem.

You might also want to monitor the process memory and handle usage through the task manager.

Steve Wranovsky