Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. TCP Connections and Threads

TCP Connections and Threads

Scheduled Pinned Locked Moved C#
sysadminjsonhelpquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SimpleData
    wrote on last edited by
    #1

    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.

    J N 2 Replies Last reply
    0
    • S SimpleData

      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.

      J Offline
      J Offline
      Jimmanuel
      wrote on last edited by
      #2

      Use asynchronous methods instead of synchronous ones (i.e. use BeginReceive instead of Receive). 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 :).

      1 Reply Last reply
      0
      • S SimpleData

        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.

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups