Why am I getting this SocketException in my C # service?

I have a service that is responsible for reading and writing from the server. Reading and writing are performed in different threads.

The reader stream is active all the time, while the write stream is activated only to send the message and dies after the message is written to the server, and the response is received.

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

System.Net.Sockets.SocketException: The socket operation could not be completed because the system did not have enough buffer space or because the queue was full

+4
source share
2 answers

This can happen if you establish a large number of outbound connections very quickly and end local ports. You can read about the "ephemeral port range" here .

Or you might just run out of sockets. Make sure you close them after you finish using them.

+6
source

I also highly recommend that you look at your recording stream and how you link to Socket related resources. That is, you call Close / Dispose for any network stream or other objects that you use with Socket? Sample code from the recording stream will be useful if you are still struggling with the problem.

You can also track process memory and handle usage through the task manager.

0
source

All Articles