Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

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

Discussion in 'Multiplayer' started by G44Gonzalo, Aug 2, 2019.

  1. G44Gonzalo

    G44Gonzalo

    Joined:
    Apr 11, 2018
    Posts:
    15
    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)



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4. using System.Net;
    5. using System.Threading;
    6. using System.Net.Sockets;
    7. using System.Collections.Generic;
    8. using System.IO;
    9. using UnityEngine.SceneManagement;
    10.  
    11. public class Servidor : MonoBehaviour
    12. {
    13.     private List<ServerClient> clientesConectados;
    14.     private List<ServerClient> clientesDesconectados;
    15.  
    16.     public bool MantenimientoDelServidor = false;
    17.     public bool StaffEnElMantenimiento = true;
    18.     private string VersionDelJuego = "Alpha.001";
    19.     public int port = 8052;
    20.     private TcpListener server;
    21.     private bool serverStarted;
    22.  
    23.     private void Start()
    24.     {
    25.         clientesConectados = new List<ServerClient>();
    26.         clientesDesconectados = new List<ServerClient>();
    27.  
    28.         try
    29.         {
    30.             server = new TcpListener(IPAddress.Any, port);
    31.             server.Start();
    32.             StartListening();
    33.             serverStarted = true;
    34.             Debug.Log("SERVER STARTED. POSSIBLE PORT: " + port);
    35.         }
    36.         catch (Exception e)
    37.         {
    38.             Debug.Log("Socket Error: " + e.Message);
    39.         }
    40.  
    41.     }
    42.  
    43.     private void Update()
    44.     {
    45.         if (!serverStarted)
    46.             return;
    47.         foreach(ServerClient c in clientesConectados)
    48.         {
    49.             if(!IsConnected(c.tcp))
    50.             {
    51.                 c.tcp.Close();
    52.                 clientesDesconectados.Add(c);
    53.                 continue;
    54.  
    55.             }
    56.             else
    57.             {
    58.                 NetworkStream s = c.tcp.GetStream();
    59.                 if(s.DataAvailable)
    60.                 {
    61.                     StreamReader reader = new StreamReader(s, true);
    62.                     string data = reader.ReadLine();
    63.  
    64.                     if (data != null)
    65.                         OnIncomingData(c, data);
    66.                 }
    67.             }
    68.  
    69.  
    70.  
    71.  
    72.         }
    73.     }
    74.  
    75.     private void OnIncomingData(ServerClient c, string data)
    76.     {
    77.         Debug.Log(c.clientName + " ha enviado el mensaje " + data);
    78.     }
    79.     private bool IsConnected(TcpClient c)
    80.     {
    81.         try
    82.         {
    83.             if (c != null && c.Client != null && c.Client.Connected)
    84.             {
    85.                 if (c.Client.Poll(0, SelectMode.SelectRead))
    86.                 {
    87.                     return !(c.Client.Receive(new byte[1], SocketFlags.Peek) == 0);
    88.                 }
    89.                 return true;
    90.             }
    91.             else
    92.                 return false;
    93.         }
    94.         catch
    95.         {
    96.             return false;
    97.         }
    98.     }
    99.     private void StartListening()
    100.     {
    101.         server.BeginAcceptTcpClient(AcceptTcpClient, server);
    102.     }
    103.     private void AcceptTcpClient(IAsyncResult ar)
    104.     {
    105.         TcpListener listener = (TcpListener)ar.AsyncState;
    106.         ServerClient AceptarCliente = new ServerClient(listener.EndAcceptTcpClient(ar));
    107.         string[] Codigo = null;
    108.         NetworkStream s = AceptarCliente.tcp.GetStream();
    109.         if (s.DataAvailable)
    110.         {
    111.             StreamReader reader = new StreamReader(s, true);
    112.             string data = reader.ReadLine();
    113.  
    114.             if (data != null)
    115.                 Codigo = data.Split('/');
    116.         }
    117.         else
    118.         {
    119.             Debug.LogError("Se ha intentado conectar un cliente sin enviar el formulario.");
    120.             return;
    121.         }
    122.         bool Cargar = false;
    123.         if(!MantenimientoDelServidor)
    124.         {
    125.             if (Codigo[0] == VersionDelJuego)
    126.                 Cargar = true;
    127.             else
    128.             {
    129.                 Send("006/ERROR: Actualiza el juego para poder jugar. Si cree que esto es un error, contacte con los administradores(g44gonzalo@gmail.com)", AceptarCliente);
    130.                 AceptarCliente.tcp.Close();
    131.             }
    132.         }
    133.         else
    134.         {
    135.             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"))
    136.                 Cargar = true;
    137.             else
    138.             {
    139.                 if (Codigo[0] != VersionDelJuego)
    140.                 {
    141.                     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);
    142.                 }
    143.                 else {
    144.                     Send("006/Lo sentimos, el servidor está en mantenimiento para todos los usuarios. Por favor pruebe a entrar en unas horas.", AceptarCliente);
    145.                 }
    146.                 AceptarCliente.tcp.Close();
    147.             }
    148.         }
    149.  
    150.  
    151.         if(Cargar)
    152.         {
    153.             AceptarCliente.clientName = Codigo[2];
    154.             clientesConectados.Add(AceptarCliente);
    155.             StartListening();
    156.         }
    157.  
    158.     }
    159.     private void Broadcast(string data, List<ServerClient> cl)
    160.     {
    161.         foreach (ServerClient c in cl)
    162.         {
    163.             try
    164.             {
    165.                 StreamWriter writer = new StreamWriter(c.tcp.GetStream());
    166.                 writer.WriteLine(data);
    167.                 writer.Flush();
    168.             }
    169.             catch(Exception e)
    170.             {
    171.                 Debug.LogError("Write error: " + e.Message + " to client " + c.clientName);
    172.             }
    173.         }
    174.     }
    175.     private void Send(string data, ServerClient c)
    176.     {
    177.         try
    178.         {
    179.             StreamWriter writer = new StreamWriter(c.tcp.GetStream());
    180.             writer.WriteLine(data);
    181.             writer.Flush();
    182.         }
    183.         catch (Exception e)
    184.         {
    185.             Debug.LogError("Write error: " + e.Message + " to client " + c.clientName);
    186.         }
    187.     }
    188. }
    189.  
    190. public class ServerClient
    191. {
    192.     public TcpClient tcp;
    193.     public string PonerNombre;
    194.     public string clientName = "Anónimo";
    195.     public string Tag;
    196.  
    197.     public ServerClient(TcpClient clientSocket)
    198.     {
    199.      
    200.         tcp = clientSocket;
    201.     }
    202. }
    203.  



    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.
     

    Attached Files:

    Last edited: Aug 6, 2019
  2. G44Gonzalo

    G44Gonzalo

    Joined:
    Apr 11, 2018
    Posts:
    15
    Deleted by me.
     
    Last edited: Aug 6, 2019
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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?
     
  4. G44Gonzalo

    G44Gonzalo

    Joined:
    Apr 11, 2018
    Posts:
    15
    Deleted by me.
     
    Last edited: Aug 6, 2019
  5. G44Gonzalo

    G44Gonzalo

    Joined:
    Apr 11, 2018
    Posts:
    15
    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 :(
     
  6. G44Gonzalo

    G44Gonzalo

    Joined:
    Apr 11, 2018
    Posts:
    15
    I need solve this, where can I ask? Is there any other place where I could ask for help?
     
  7. G44Gonzalo

    G44Gonzalo

    Joined:
    Apr 11, 2018
    Posts:
    15
    I reported this an error. I hope they can help me there. This error is very rare. Thanks for all.