unity socket bug or Threadpool bug ? please help,,
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.
I added the code.
and
Calling Threading.ThreadPool.GetMaxThreads(out int workerThreads, out int
completionPortThreads) in Unity Project
C# Project : workerThreads:1023, completionPortThreads :1000
Unity Project : workerThreads:800, completionPortThreads :200
calling ThreadPool.SetMaxThreads(1023, 1000) in unity project.
But the phenomenon is the same...
thank.
Your answer
Follow this Question
Related Questions
Missing Shadows on Some Objects 0 Answers
Particle Collision Bug. 1 Answer
Unity Keeps Destroying My Projects. Please help! (Urgent) 1 Answer
Player keeps bumping when walking over to another sprite 1 Answer
Light Shadow problem 1 Answer