Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by leethfs · Sep 23, 2020 at 04:25 AM · bugthreadssocket

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Sep 23, 2020 at 09:24 AM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image leethfs · Sep 23, 2020 at 11:06 AM 0
Share

hi

I added the code.

and

Calling Threading.ThreadPool.Get$$anonymous$$axThreads(out int workerThreads, out int

completionPortThreads) in Unity Project

C# Project : workerThreads:1023, completionPortThreads :1000

Unity Project : workerThreads:800, completionPortThreads :200

calling ThreadPool.Set$$anonymous$$axThreads(1023, 1000) in unity project.

But the phenomenon is the same...

Thanks for your interest.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

158 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

JavaScript TcpClient().Connect freezes 1 Answer

Using Job System to Poll Sockets 0 Answers

unity network socket bug? or threadpool bug? please help,, 0 Answers

PC can't make socket connections after running game in editor 0 Answers

The type name 'UnityWebRequest' could not be found in the namespace 'UnityEngine.Networking'. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges