TCP Connections and Threads
-
Hi, I have been thinking... Let's assume that I have a server application and lots of clients all over the world connect to my server by using TCP protocol. When a client sends me a connection request, my server application creates a thread for the client and do the rest of the communication in that thread. As far as I know, after some number of threads, operating system starts to get mad and refuses to create more threads. This causes my application to deny to service. How is this problem solved? Am I missing a point? Thanks in advance.
-
Hi, I have been thinking... Let's assume that I have a server application and lots of clients all over the world connect to my server by using TCP protocol. When a client sends me a connection request, my server application creates a thread for the client and do the rest of the communication in that thread. As far as I know, after some number of threads, operating system starts to get mad and refuses to create more threads. This causes my application to deny to service. How is this problem solved? Am I missing a point? Thanks in advance.
Use asynchronous methods instead of synchronous ones (i.e. use
BeginReceive
instead ofReceive
). This allows you to not have to explicitly create/destroy a thread for each connected client; instead, the async callbacks are executed on Thread Pool threads which are managed by the runtime. The code required to implement an async client/server can be more complex than the alternative but it solves this specific problem. Once you spend some time and really get familiar with the async way of doing things the complexity will cease to be a factor and you won't know how you lived without it :). -
Hi, I have been thinking... Let's assume that I have a server application and lots of clients all over the world connect to my server by using TCP protocol. When a client sends me a connection request, my server application creates a thread for the client and do the rest of the communication in that thread. As far as I know, after some number of threads, operating system starts to get mad and refuses to create more threads. This causes my application to deny to service. How is this problem solved? Am I missing a point? Thanks in advance.
As suggested, asynchronous methods can solve this problem. But you should be very careful when using it. It can make your code unnecessarily complicated. IMO, you should choose it only when it is necessary. I'd suggest to use a thread pool rather than creating new thread each time. It should scale well in most scenarios. For more scalability than that, asynchronous method is the way to go.
Best wishes, Navaneeth