Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Lot's of Asynchronous TCP Connections

Discussion in 'Multiplayer' started by ToshoDaimos, Feb 3, 2021.

  1. ToshoDaimos

    ToshoDaimos

    Joined:
    Jan 30, 2013
    Posts:
    679
    I'm making a custom message system on top of TCP NetworkStream. I would like to use asynchronous Connect/Read/Write methods. I read that those operations spawn a new thread. Is it always a new thread per operation? If I have 10k TCP connections, then will I have 10k+ read/write threads? I'm asking because I want to have the same basic network codebase for my client and server. On the client there is no problem, but on the server I may have to handle 10k+ connections since my game is F2P. Ideally I would like to have a single background thread handling all read/write operations for all TCP connections. Each thread has about 1MB of stack memory, which means that 10k threads take 10GB or RAM just for stack.
     
  2. islaitala

    islaitala

    Joined:
    Sep 6, 2017
    Posts:
    3
    Why would you have 10k+ connections to a single server?
     
  3. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,329
    async tcp does not scale in Unity.
    it'll choke around 150 connections, I wouldn't bother with it.

    for Mirror's tcp transport we had to use the old model of 2 threads per connection. that's still the best scaling tcp transport we have.

    tried everything from async-await, beginsend, socketasynceventargs, etc.