Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

301
Visualizações
Sending data to and receiving data from localhost in Unity caused "IOException: Too many open files" error

To test the networking part of my Unity application, I have an "imitatee" game object that moves according to the keyboard input. The networking manager component (1) gets the movement data of the "imitatee" and sends it to a node.js localhost server using PUT and also (2) gets back the from localhost and applies it to the "imitator" object. The program worked fine, but after a while I got the error:

IOException: Too many open files

A window also popped up in the Unity editor saying that "Opening files failed" and then the editor crashed. I think that it was caused by memory not being disposed properly but have not figured out exactly how. Here is the GameManager code:

NetworkingManager.cs

public class Data
{
    public float posX;
    ...

    public Data(float posX...)
    {
        this.posX = posX;
        ...
    }
}

public class NetworkingManager : MonoBehaviour
{

    private GameObject imitator;
    private GameObject imitatee;

    private Data data;
    private string jsonData;
    private Data receivedData;

    private string uri = "http://localhost:3000/";

    private void Awake()
    {
        // Assign references
        ...
    }

    private IEnumerator SendData()
    {
        data = new Data(
            imitatee.transform.position.x,
            ...
            );

        jsonData = JsonUtility.ToJson(data);

        using (UnityWebRequest req = UnityWebRequest.Put(uri, jsonData))
        {
            req.SetRequestHeader("Content-Type", "application/json");
            yield return req.SendWebRequest();
    

            if (req.isNetworkError || req.isHttpError)
            {
                Debug.Log(req.error);
            }
        }

    }

    private IEnumerator ReceiveData()
    {

        using (UnityWebRequest req = UnityWebRequest.Get(uri))
        {
            yield return req.SendWebRequest();

            if (req.isNetworkError)
            {
                Debug.Log(req.error);
            }
            else
            {
                receivedData = JsonUtility.FromJson<Data>(req.downloadHandler.text);

                imitator.transform.position = new Vector3(
                    receivedData.posX,
                    ...
                    );

                ...
            }
        }
        
    }

    void Update()
    {
        StartCoroutine(SendData());
        StartCoroutine(ReceiveData());
    }
}

Besides, there was no error logged for the node.js backend.

backend.js

const express = require('express')
const app = express()
app.use(express.json())
const port = 3000
var data

app.get('/', (req, res) => {
  if (data) res.json(data)
})

app.put('/', (req, res) => {
  data = req.body
  console.log("Received " + req.body) 
})

app.listen(port, () => {
  console.log(`App listening at http://localhost:${port}`)
})

Appreciate your help!

over 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda