Unity Thread or socket bug? please regard.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;

class Client
{
    private Socket socket;
    private byte[] buffer = new byte[1024];
    private StringBuilder sb = new StringBuilder();
    public Client(Socket socket)
    {
        this.socket = socket;
        this.socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new System.AsyncCallback(Receive), this);
    }


    private void Receive(IAsyncResult result)
    {
        if (socket.Connected)
        {
            int size = 0;
            try
            {
                this.socket.EndReceive(result);
            }
            catch (Exception e)
            {
                UnityEngine.Debug.Log(e.Message);
                return;
            }

            this.socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, Receive, this);
        }
    }
}

class ServerSocket : Socket
{
    public ServerSocket() : base(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    {
        base.Bind(new IPEndPoint(IPAddress.Any, 7902));
        base.Listen(0);

        BeginAccept(Accept, this);
    }

    private void Accept(IAsyncResult result)
    {
        UnityEngine.Debug.Log("ServerSocket threadID:" + System.Threading.Thread.CurrentThread.ManagedThreadId);
        var client = new Client(EndAccept(result));
        BeginAccept(Accept, this);
    }
}

public class Test : MonoBehaviour
{
    void Start()
    {
        new ServerSocket();
    }

    public static void OpenHandler(Network.Session.NetSession session)
    {
    }

    // Update is called once per frame
    void Update()
    {
    }
}

hi

We have question about Unity Socket and ThreadPool.

Server code was written in the Unity project and processed by Asynchronous.

AsyncCode, such as “BeginAccept”, “BeginRecive”, is written,
and we wrote a code that only requires the connection with a bot simply.

But we run servers within Unity if it has more than 1,000 connection counts,
WookerThread by increases momentarily, and the connection fails.

If we turn the server in the C# project with the same code as above, there is no problem.
But if we run the server in Unity, that kind of problem will happen.

If we comment “BeginReceive” code in the code, it connects normally.

We think it’s a Threadpool issue.
The “ThreadPool.SetMaxThreads” function was called to control the number of threads.

Thread does not increase when the function was called, but if more than 1000 connections, the problem occurred.

There’s nothing wrong with being under 1,000.

Then we accept, about 4 types of Thread ids are process,
and if more than 1,000 connections ( around 1,030) had connected, the Thread would rapidly increase and can’t connect.

We don’t know if this is a Unity-related Bug or a user problem.

We are looking forward to you and hope you can help with this problem.

thank.

Well, It looks like that 1000 (or 1024?) is the default value for the max thread value for I/O threads.

Try reading GetMaxThreads and log the two values to see if you get any differences between Unity and C#. Keep in mind that Unity can use several different backends and the default limits might be different from case to case.

Anyways, if you need more than 1000 concurrent connections you may want to increase the max thread count for I/O threads in the pool. If you don’t actually have 1000 concurrent connections you may have a resource management issue in your code. It’s hard to tell without seeing any code.