Search Unity

Trying to make basic connection.

Discussion in 'Multiplayer' started by User340, Oct 1, 2010.

  1. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    I'm trying to connect two machines together. I know that I can't just connect immediately without doing some testing beforehand, like for NAT. I got this script from the Unity documentation. I think it's supposed to determine if useNAT should be on or off, but some parts look a little messed up to me.

    1) The line that says "case ConnectionTesterStatus.NATpunchthroughAddressRestrictedCone:" executes no code.
    2) There are 2 cases that say "ConnectionTesterStatus.LimitedNATPunchthroughPortRestricted:"

    As of right now I am unable to establish a connection with this script. Can someone update it?

    Also I'm wondering if useNAT is the only thing I need to worry about to guarantee a connection. What are all the variables I need to consider? Because I know that some connections (like 3G) can't initialize a server, right?

    Code (csharp):
    1. var testStatus = "Testing network connection capabilities.";
    2. var testMessage = "Test in progress";
    3. var shouldEnableNatMessage : String = "";
    4. var doneTesting = false;
    5. var probingPublicIP = false;
    6. var serverPort = 9999;
    7. var connectionTestResult = ConnectionTesterStatus.Undetermined;
    8.  
    9. // Indicates if the useNat parameter be enabled when starting a server
    10. var useNat = false;
    11.  
    12. function OnGUI() {
    13.     GUILayout.Label("Current Status: " + testStatus);
    14.     GUILayout.Label("Test result : " + testMessage);
    15.     GUILayout.Label(shouldEnableNatMessage);
    16.     if (!doneTesting)
    17.         TestConnection();
    18. }
    19.  
    20. function TestConnection() {
    21.     // Start/Poll the connection test, report the results in a label and
    22.     // react to the results accordingly
    23.     connectionTestResult = Network.TestConnection();
    24.     switch (connectionTestResult) {
    25.         case ConnectionTesterStatus.Error:
    26.             testMessage = "Problem determining NAT capabilities";
    27.             doneTesting = true;
    28.             break;
    29.            
    30.         case ConnectionTesterStatus.Undetermined:
    31.             testMessage = "Undetermined NAT capabilities";
    32.             doneTesting = false;
    33.             break;
    34.                        
    35.         case ConnectionTesterStatus.PublicIPIsConnectable:
    36.             testMessage = "Directly connectable public IP address.";
    37.             useNat = false;
    38.             doneTesting = true;
    39.             break;
    40.            
    41.         // This case is a bit special as we now need to check if we can
    42.         // circumvent the blocking by using NAT punchthrough
    43.         case ConnectionTesterStatus.PublicIPPortBlocked:
    44.             testMessage = "Non-connectble public IP address (port " +
    45.                 serverPort +" blocked), running a server is impossible.";
    46.             useNat = false;
    47.             // If no NAT punchthrough test has been performed on this public
    48.             // IP, force a test
    49.             if (!probingPublicIP) {
    50.                 connectionTestResult = Network.TestConnectionNAT();
    51.                 probingPublicIP = true;
    52.                 testStatus = "Testing if blocked public IP can be circumvented";
    53.                 timer = Time.time + 10;
    54.             }
    55.             // NAT punchthrough test was performed but we still get blocked
    56.             else if (Time.time > timer) {
    57.                 probingPublicIP = false;         // reset
    58.                 useNat = true;
    59.                 doneTesting = true;
    60.             }
    61.             break;
    62.         case ConnectionTesterStatus.PublicIPNoServerStarted:
    63.             testMessage = "Public IP address but server not initialized, "+
    64.                 "it must be started to check server accessibility. Restart "+
    65.                 "connection test when ready.";
    66.             break;
    67.            
    68.         case ConnectionTesterStatus.LimitedNATPunchthroughPortRestricted:
    69.             testMessage = "Limited NAT punchthrough capabilities. Cannot "+
    70.                 "connect to all types of NAT servers.";
    71.             useNat = true;
    72.             doneTesting = true;
    73.             break;
    74.        
    75.         case ConnectionTesterStatus.LimitedNATPunchthroughPortRestricted:
    76.             testMessage = "Limited NAT punchthrough capabilities. Cannot "+
    77.                 "connect to all types of NAT servers. Running a server "+
    78.                 "is ill advised as not everyone can connect.";
    79.             useNat = true;
    80.             doneTesting = true;
    81.             break;
    82.            
    83.         case ConnectionTesterStatus.LimitedNATPunchthroughSymmetric:
    84.             testMessage = "Limited NAT punchthrough capabilities. Cannot "+
    85.                 "connect to all types of NAT servers. Running a server "+
    86.                 "is ill advised as not everyone can connect.";
    87.             useNat = true;
    88.             doneTesting = true;
    89.             break;
    90.        
    91.         case ConnectionTesterStatus.NATpunchthroughAddressRestrictedCone:
    92.         case ConnectionTesterStatus.NATpunchthroughFullCone:
    93.             testMessage = "NAT punchthrough capable. Can connect to all "+
    94.                 "servers and receive connections from all clients. Enabling "+
    95.                 "NAT punchthrough functionality.";
    96.             useNat = true;
    97.             doneTesting = true;
    98.             break;
    99.  
    100.         default:
    101.             testMessage = "Error in test routine, got " + connectionTestResult;
    102.     }
    103.     if (doneTesting) {
    104.         if (useNat)
    105.             shouldEnableNatMessage = "When starting a server the NAT "+
    106.                 "punchthrough feature should be enabled (useNat parameter)";
    107.         else
    108.             shouldEnableNatMessage = "NAT punchthrough not needed";
    109.         testStatus = "Done testing";
    110.     }
    111. }