Why are my sockets closing after a certain of time?


I would like to know why the sockets i'm opening are closing after a few seconds/minutes.

On the server side i have a UI which shows me the amount of opened connections. So whenever i run the code mentioned below, the amount starts to increase. So when i reach for example 5000 open connections suddenly a X amount of connections drop. No idea why this is happening.

I'm running the method on different threads to speed up the process.

Which problems do i have?

  • The sockets are closing automatically after a few seconds/minutes depending on the amount of threads.
  • Sometimes it is throwing a SocketException, this happens if i increase the amount of threads.

    System.Net.Sockets.SocketException (0x80004005): A connection attempt failed because the connected party did not respond correctly after a certain time, or the connection made failed because the connected host did not respond

    static void Main(string[] args)
        for (int i = 0; i < 100; i++)
            Thread thread = new Thread(HitOne);
    static void HitOne()
             * Sleep to prevent network from losing connection
             * (Not sure if this is correct)
                TcpClient client = new TcpClient(IP, PORT);
            catch (SocketException e)

Is it normal that a socket closes after a certain time? At the beginning i was thinking that a huge amount of threads and connections could drop my network (like a ddos attack would do). I thought maybe the sleep method would solve this issue but it isn't.

  • I can't add my server here. The code it way too complex. But i can say that i don't have functionality to closes incoming connections. So normally everything should stay up.

  • If you think there is a better way to do this, please let me know.

Thanks in advance.

asked on Stack Overflow Oct 16, 2019 by J. Adam

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0