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.