[Resolved] Help, unity server does not use the established port. It uses one random.

Resolved: If you mark the development build box, the bug is active. I reported this.

Edit: I deleted some things, because I reported this on Report errors. However if anyone wants to help me, here is a mor simply code, and it gives the error too.

(My english is not very good)

I have been developing an online game for a few months now, and I have even tried it and it has always worked perfectly.
The problem is that suddenly, when running the server, it takes a random port instead of 8052.

(TCP server)

using UnityEngine;
using System.Collections;
using System;
using System.Net;
using System.Threading;
using System.Net.Sockets;
using System.Collections.Generic;
using System.IO;
using UnityEngine.SceneManagement;

public class Servidor : MonoBehaviour
{
    private List<ServerClient> clientesConectados;
    private List<ServerClient> clientesDesconectados;

    public bool MantenimientoDelServidor = false;
    public bool StaffEnElMantenimiento = true;
    private string VersionDelJuego = "Alpha.001";
    public int port = 8052;
    private TcpListener server;
    private bool serverStarted;

    private void Start()
    {
        clientesConectados = new List<ServerClient>();
        clientesDesconectados = new List<ServerClient>();

        try
        {
            server = new TcpListener(IPAddress.Any, port);
            server.Start();
            StartListening();
            serverStarted = true;
            Debug.Log("SERVER STARTED. POSSIBLE PORT: " + port);
        }
        catch (Exception e)
        {
            Debug.Log("Socket Error: " + e.Message);
        }

    }

    private void Update()
    {
        if (!serverStarted)
            return;
        foreach(ServerClient c in clientesConectados)
        {
            if(!IsConnected(c.tcp))
            {
                c.tcp.Close();
                clientesDesconectados.Add(c);
                continue;

            }
            else
            {
                NetworkStream s = c.tcp.GetStream();
                if(s.DataAvailable)
                {
                    StreamReader reader = new StreamReader(s, true);
                    string data = reader.ReadLine();

                    if (data != null)
                        OnIncomingData(c, data);
                }
            }




        }
    }

    private void OnIncomingData(ServerClient c, string data)
    {
        Debug.Log(c.clientName + " ha enviado el mensaje " + data);
    }
    private bool IsConnected(TcpClient c)
    {
        try
        {
            if (c != null && c.Client != null && c.Client.Connected)
            {
                if (c.Client.Poll(0, SelectMode.SelectRead))
                {
                    return !(c.Client.Receive(new byte[1], SocketFlags.Peek) == 0);
                }
                return true;
            }
            else
                return false;
        }
        catch
        {
            return false;
        }
    }
    private void StartListening()
    {
        server.BeginAcceptTcpClient(AcceptTcpClient, server);
    }
    private void AcceptTcpClient(IAsyncResult ar)
    {
        TcpListener listener = (TcpListener)ar.AsyncState;
        ServerClient AceptarCliente = new ServerClient(listener.EndAcceptTcpClient(ar));
        string[] Codigo = null;
        NetworkStream s = AceptarCliente.tcp.GetStream();
        if (s.DataAvailable)
        {
            StreamReader reader = new StreamReader(s, true);
            string data = reader.ReadLine();

            if (data != null)
                Codigo = data.Split('/');
        }
        else
        {
            Debug.LogError("Se ha intentado conectar un cliente sin enviar el formulario.");
            return;
        }
        bool Cargar = false;
        if(!MantenimientoDelServidor)
        {
            if (Codigo[0] == VersionDelJuego)
                Cargar = true;
            else
            {
                Send("006/ERROR: Actualiza el juego para poder jugar. Si cree que esto es un error, contacte con los administradores(g44gonzalo@gmail.com)", AceptarCliente);
                AceptarCliente.tcp.Close();
            }
        }
        else
        {
            if (Codigo[0] == VersionDelJuego && StaffEnElMantenimiento && (Codigo[1] == "Helper" || Codigo[1] == "VHelper" || Codigo[1] == "Mod" || Codigo[1] == "VMod" || Codigo[1] == "Admin" || Codigo[1] == "VAdmin" || Codigo[1] == "Owner"))
                Cargar = true;
            else
            {
                if (Codigo[0] != VersionDelJuego)
                {
                    Send("006/Lo sentimos, el servidor está en mantenimiento. Además tiene el juego desactualizado. Si es parte del staff y quiere entrar, por favor actualice el juego.", AceptarCliente);
                }
                else {
                    Send("006/Lo sentimos, el servidor está en mantenimiento para todos los usuarios. Por favor pruebe a entrar en unas horas.", AceptarCliente);
                }
                AceptarCliente.tcp.Close();
            }
        }


        if(Cargar)
        {
            AceptarCliente.clientName = Codigo[2];
            clientesConectados.Add(AceptarCliente);
            StartListening();
        }

    }
    private void Broadcast(string data, List<ServerClient> cl)
    {
        foreach (ServerClient c in cl)
        {
            try
            {
                StreamWriter writer = new StreamWriter(c.tcp.GetStream());
                writer.WriteLine(data);
                writer.Flush();
            }
            catch(Exception e)
            {
                Debug.LogError("Write error: " + e.Message + " to client " + c.clientName);
            }
        }
    }
    private void Send(string data, ServerClient c)
    {
        try
        {
            StreamWriter writer = new StreamWriter(c.tcp.GetStream());
            writer.WriteLine(data);
            writer.Flush();
        }
        catch (Exception e)
        {
            Debug.LogError("Write error: " + e.Message + " to client " + c.clientName);
        }
    }
}

public class ServerClient
{
    public TcpClient tcp;
    public string PonerNombre;
    public string clientName = "Anónimo";
    public string Tag;

    public ServerClient(TcpClient clientSocket)
    {
     
        tcp = clientSocket;
    }
}

Edit: Now nothing is censored. Get this code, and put it on an empty scene, with only 1 gameobject. And build it.
If something else is needed, ask me and I will publish it.

Please help. Thanks for reading.

Deleted by me.

The problem code appears to be somewhere you’re not showing. The first Debug.Log statement from the code you posted which appears in your log if far after where the problem appears to occur.

Where is the code which is writing all this to the log? How is that getting written before the “Initialize engine version” line?

Deleted by me.

Hey, i think its not the code because I have an older backup of the project, and the older version worked very well at his moment, but today I started it and it starts on the same port, like the actual version. I think is windows or anything, but i dont know what is it. I disabled avast and tried another things, but the server still wrong… Please help :frowning:

I need solve this, where can I ask? Is there any other place where I could ask for help?

I reported this an error. I hope they can help me there. This error is very rare. Thanks for all.