Author Topic: Custom server problem  (Read 2421 times)

sloopernine

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 47
    • View Profile
Custom server problem
« on: August 11, 2013, 10:52:35 AM »
Hi

I am trying to make my own server instead of the one that came with the TNet package.

When I start the server and the same program are trying to connect I get error (translated) "Failed to make a connection because the target machine actively refused it."

But when I start the server.exe that came with TNet and press "start server" in my program it works...

Sorry for the messy code...

  1. using UnityEngine;
  2. using System.IO;
  3. using System.Collections;
  4. using System; // Required for namespace Exception used in Create Server menu
  5. using TNet;
  6.  
  7. [RequireComponent(typeof(TNManager))]
  8. public class ServerSetup : MonoBehaviour
  9. {
  10.     //static public TNAutoJoin instance;
  11.  
  12.     // The window id, changeable in editor.
  13.     public int windowId = 1;
  14.  
  15.     // GUI window.
  16.     private Rect windowRect = new Rect(10, 10, 150, 25);
  17.  
  18.     // Server preset values.
  19.     private string serverName = "Castro";
  20.     private string maxPlayers = "32";
  21.     private string tcpPort = "25566";
  22.     private int udpPort;
  23.     private string serverToJoinIP = "127.0.0.1";
  24.  
  25.     public string level = "City";
  26.     public int channelID = 1;
  27.  
  28.     public string successFunctionName;
  29.     public string failureFunctionName;
  30.  
  31.     public enum MenuState
  32.     {
  33.         serverOff,
  34.         serverOn
  35.     }
  36.     public MenuState currMenuState;
  37.  
  38.     public AudioClip buttonClick;
  39.  
  40.     private ServerLog sLog;
  41.  
  42.     /// <summary>
  43.     /// Set the instance so this script can be easily found.
  44.     /// </summary>
  45.  
  46.     //void Awake() { if (instance == null) instance = this; }
  47.  
  48.         // Use this for initialization
  49.         void Start ()
  50.     {
  51.             sLog = GetComponent<ServerLog>();
  52.  
  53.         udpPort = UnityEngine.Random.Range(10000, 40000);
  54.         }
  55.        
  56.         // Update is called once per frame
  57.         void Update ()
  58.     {
  59.         if (TNServerInstance.isActive)
  60.         {
  61.             currMenuState = MenuState.serverOn;
  62.         }
  63.         else
  64.         {
  65.             currMenuState = MenuState.serverOff;
  66.         }
  67.         }
  68.  
  69.     void OnGUI()
  70.     {
  71.         windowRect = GUILayout.Window(windowId, windowRect, windowFunc, "Start/Stop Server");
  72.     }
  73.  
  74.     private void windowFunc(int id)
  75.     {
  76.         switch (currMenuState)
  77.         {
  78.             case MenuState.serverOn:
  79.  
  80.                 GUILayout.Label("Server Name");
  81.                 GUILayout.Box(serverName);
  82.  
  83.                 GUILayout.Label("TCP Port");
  84.                 GUILayout.Box(tcpPort);
  85.  
  86.                 GUILayout.Label("UDP Port");
  87.                 GUILayout.Box("" + udpPort);
  88.  
  89.                 GUILayout.Label("Max Players");
  90.                 GUILayout.Box(maxPlayers);
  91.  
  92.                 if (GUILayout.Button("Disconnect"))
  93.                 {
  94.                     TNManager.Disconnect();
  95.                     TNServerInstance.Stop();
  96.                     TNManager.LoadLevel("Server");
  97.                     sLog.MessageBoxString("Server stopped");
  98.                 }
  99.  
  100.                 break;
  101.  
  102.             case MenuState.serverOff:
  103.  
  104.                 GUILayout.Label("Server Name");
  105.                 serverName = GUILayout.TextField(serverName);
  106.  
  107.                 GUILayout.Label("TCP Port");
  108.                 tcpPort = GUILayout.TextField(tcpPort);
  109.  
  110.                 GUILayout.Label("UDP Port");
  111.                 GUILayout.Label("" + udpPort);
  112.  
  113.                 GUILayout.Label("Max Players");
  114.                 maxPlayers = GUILayout.TextField(maxPlayers);
  115.  
  116.                 if (GUILayout.Button("Create Server"))
  117.                 {
  118.                     //audio.clip = buttonClick;
  119.                     //audio.Play();
  120.  
  121.                     // Try to create server.
  122.                     try
  123.                     {
  124.                         // TNet start server.
  125.                         TNServerInstance.Start(int.Parse(tcpPort), udpPort);
  126.  
  127.                         TNManager.Connect(serverToJoinIP);
  128.  
  129.                         //TNManager.LoadLevel(level);
  130.  
  131.                         // Joins/Starts channel + 1 for excluding the server as a player and make that slot free.
  132.                         //TNManager.JoinChannel(channelID, "City", false, int.Parse(maxPlayers) + 1, "");
  133.  
  134.                         //MessageBox("Server started!");
  135.                         //MessageBox("Server joined channel: " + TNManager.isInChannel);
  136.  
  137.                     }
  138.                     // Something went wrong with creating the server.
  139.                     catch (Exception)
  140.                     {      
  141.                         //MessageBox("Something went wrong, check your settings");
  142.                     }
  143.                 }
  144.                 if (GUILayout.Button("Quit"))
  145.                 {
  146.                     Application.Quit();
  147.                 }
  148.      
  149.                 break;
  150.         }
  151.     }
  152.  
  153.     /// <summary>
  154.     /// On success -- join a channel.
  155.     /// </summary>
  156.  
  157.     void OnNetworkConnect(bool result, string message)
  158.     {
  159.         if (result)
  160.         {
  161.             sLog.MessageBoxString("Connected to server, trying to create channel...");
  162.             TNManager.JoinChannel(channelID, level);
  163.             sLog.MessageBoxString("Channel created!");
  164.         }
  165.         else if (!string.IsNullOrEmpty(failureFunctionName))
  166.         {
  167.             UnityTools.Broadcast(failureFunctionName, message);
  168.             sLog.MessageBoxString("Failed to create channel: \n" + message);
  169.         }
  170.         else
  171.         {
  172.             sLog.MessageBoxString("Failed to create channel: \n" + message);
  173.             Debug.LogError(message);
  174.         }
  175.     }
  176.  
  177.     /// <summary>
  178.     /// Joined a channel (or failed to).
  179.     /// </summary>
  180.  
  181.     void OnNetworkJoinChannel(bool result, string message)
  182.     {
  183.         if (result)
  184.         {
  185.             sLog.MessageBoxString("Joined channel " + channelID + " with success!");
  186.         }
  187.         else
  188.         {
  189.             sLog.MessageBoxString("Failed to join channel: " + message);
  190.             sLog.MessageBoxString("Starting to close down server...");
  191.  
  192.             TNManager.Disconnect();
  193.                 sLog.MessageBoxString("Detached from server...");
  194.  
  195.             TNServerInstance.Stop();
  196.                 sLog.MessageBoxString("Server stopped...");
  197.  
  198.             TNManager.LoadLevel("Server");
  199.                 sLog.MessageBoxString("Server close down finnished!");
  200.         }
  201.     }
  202. }
  203.  

sloopernine

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 47
    • View Profile
Re: Custom server problem
« Reply #1 on: August 11, 2013, 11:25:10 AM »
Ok I got it working, seems like I have to open up the correct ports on my router, when not using the standalone server.exe

Why? Is there more things going on in the server.exe ?
« Last Edit: August 11, 2013, 12:50:15 PM by sloopernine »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom server problem
« Reply #2 on: August 11, 2013, 02:29:56 PM »
TNServerInstance.Start function calls TNServerInstance.StartLocal, which opens up the ports on the router inside, assuming it was able to start the server to begin with. TNServer.exe uses the same code.