Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity: Can Connect Only on localhost, no External IP

Discussion in 'Multiplayer' started by scippo97, Jan 20, 2013.

  1. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    So, I'm trying to make an online game, and I'm working on the online (Duh), but I can only connect with localhost, if I use External IP Or another computer does it fails.

    I DO NOT want to use Master Server, because i'm planning to make the online server free for all, so they can Set-Up their own Server

    and Yes, I got portforward already, I tried with several ports, 2550, 25600, all with no luck.

    I already tries UseNAT, and the connection test script says everyone can connect to me.

    to end this, here's the full script:

    Code (csharp):
    1. //Strings
    2. private var ServerPortString : String;
    3. private var NOfPlayersServerString : String;
    4. var ClientPortString : String;
    5. var Ip : String;
    6.  
    7. //ints
    8.  
    9. var ServerPort : int;
    10. var NOfPlayersServer : int;
    11. private var playerCount: int = 0;
    12. var ClientPort : int;
    13.  
    14. //true/false
    15. var IsCreatingServer = true;
    16. var IsConnectingToClient = false;
    17. var IsOnMainMenu = false;
    18.  
    19.  
    20. function Awake () {
    21.     DontDestroyOnLoad (transform.gameObject);
    22. }
    23.  
    24. function StartServer() {
    25. Network.InitializeServer(NOfPlayersServer, ServerPort, true);
    26. MasterServer.RegisterHost("Worldz", "Test Server", "I don't know what the hell is this.");
    27. }
    28.  
    29. function OnGUI() {
    30.    
    31.     if(IsCreatingServer){
    32.     ServerPortString = GUI.TextField (Rect (Screen.width/2-150,Screen.height/1.5-20, 300, 20), ServerPortString, 25);
    33.     NOfPlayersServerString = GUI.TextField (Rect (Screen.width/2-150,Screen.height/1.3-20, 300, 20), NOfPlayersServerString, 25);
    34.     ServerPort = int.Parse(ServerPortString);
    35.     NOfPlayersServer = int.Parse(NOfPlayersServerString);
    36.     if(GUI.Button (Rect (Screen.width/2-100,Screen.height/1.4-25,200,50),"Create Server")){
    37.     StartServer();
    38.         }
    39.     }
    40.    
    41.     if(IsConnectingToClient){
    42.     Ip = GUI.TextField (Rect (Screen.width/2-150,Screen.height/1.5-20, 300, 20), Ip, 25);
    43.     ClientPortString = GUI.TextField (Rect (Screen.width/2-150,Screen.height/1.2-20, 300, 20), ClientPortString, 25);
    44.     ClientPort = int.Parse(ClientPortString);
    45.     if(GUI.Button (Rect (Screen.width/2-100,Screen.height/2-25,200,50),"Connect to the Server")){
    46.     ConnectToServer();
    47.             }
    48.     if(GUI.Button (Rect (Screen.width/2-100,Screen.height/1.3-25,200,50),"Back")){
    49.     IsConnectingToClient = false;
    50.     IsOnMainMenu = true;
    51.             }  
    52.         }
    53.    
    54.     if(IsOnMainMenu){
    55.     if(GUI.Button (Rect (Screen.width/2-100,Screen.height/2-25,200,50),"Connect to a Server")){
    56.     IsOnMainMenu = false;
    57.     IsConnectingToClient = true;
    58.         }
    59.     if(GUI.Button (Rect (Screen.width/2-100,Screen.height/1.5-25,200,50),"Create a Server")){
    60.     IsOnMainMenu = false;
    61.     IsCreatingServer = true;
    62.         }
    63.     }
    64. }
    65.  
    66. function ConnectToServer() {
    67. Network.Connect(Ip,ClientPort);
    68. }
    69.  
    70. function Update() {
    71. if(ServerPortString == null){
    72. ServerPortString = ("0");
    73. }
    74. if(ClientPortString == null){
    75. ServerPortString = ("0");
    76. }
    77. if(NOfPlayersServerString == null){
    78. NOfPlayersServerString = ("0");
    79.     }
    80. }
    81.  
    82. function OnServerInitialized() {
    83.     Debug.Log("Server initialized and ready");
    84. }
    85.  
    86. function OnPlayerConnected(player: NetworkPlayer) {
    87.     Debug.Log("Player " + playerCount++ +
    88.               " connected from " + player.ipAddress +
    89.               ":" + player.port);
    90.     // Populate a data structure with player information ...
    91. }
    92.  
    93. function OnPlayerDisconnected(player: NetworkPlayer) {
    94.     Debug.Log("Clean up after player " +  player);
    95.     Network.RemoveRPCs(player);
    96.     Network.DestroyPlayerObjects(player);
    97. }
     
  2. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    open the port in your router/firewall.
     
  3. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    Just to tell, I opened ALL the possible porta and tries 3 modems, ALL with no luck at all, connects only with localhost.
     
    Last edited: Jan 20, 2013
    MilenaRocha likes this.
  4. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    If you cant connect from the outside, it is most likely a port somewhere (router, firewall, ISP?) that is blocked.
     
  5. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    what ports does Unity use? As I already said, I portforwarded port 80, 2550, 26500 and 30000, tried every one of these, with no luck.
     
  6. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    why would it be any of those ports? which port are you connecting on?
     
  7. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    I tried to connect to everyone of these one at one, and even if they are portforwarded unity refuse to connect.
     
  8. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    Any help? I tried everything, even setting up no-ip, but it refuses to connect using external IP, AND YES, MY PORTS ARE UNLOCKED for god's sake.
     
  9. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    var ServerPort : int; -> your port is right there, thats the port that should be open and forwarded if needed.
     
  10. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    Already done, i opened and forwarded and everything else the port I try to use, and it failed, so I tried to do the same with another port, and it failed again, so basicly unity refuses to connect using an external IP.

    EDIT: For god's sake, people refuse to understand, so I made a pic.

    (AGAIN, the port is opened, forwarded and everything else.

    EDIT2: (here's the link to the image if it's too small http://s7.postimage.org/mgjgtctrf/Cattura_di_schermata_1.png

    PS( I made this script all by myself, I sure know what are the vars in there, just to say.)
     
    Last edited: Jan 23, 2013
  11. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    If it works locally but not through your external IP, it is still most likely what I said: The port is blocked *somewhere*, maybe by your ISP, maybe by your router, maybe by your firewall on your computer, etc. Maybe you only opened in one place, maybe you dont have control over the place where it is blocked (at your ISP).
     
  12. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    I can assure, the port is not blocked, I already tried all of them using other tools..... and those tools work just fine, but Unity doesn't, it refuses in every way to work, and I already called my ISP to be sure they don't block something, and they don't block anything, so I am starting to think unity hates me.
     
  13. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Turn on full network debugging in the editor and try and find what is happening, it should show something. And sorry for not understanding, just trying to troubleshoot.
    Post the debug if possible.
     
  14. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    already turned on. the debug is all in the picture.

    (here's the server debug in image: http://s1.postimage.org/7uwawo0r3/Cattura_di_schermata_2.png )

    (the client debug is in the pic up there.)
     
    Last edited: Jan 23, 2013
  15. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Ok, thats the server end. What about the connecting client side ?
     
  16. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
  17. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    I guess your problem is the nat traversal. Do you have way to host the game on a public ip without router of firewall ?
    So the remote connects directly to the game port.
     
  18. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    public ip? what's that?
     
  19. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    When you connect to the internet, your provider will assign you an ip addres, hopfully this isn't an RFC1918 address.
    If you get an ip from a public range, that is routed on the net, you can connect to it directly when you connect the pc or laptop directly on the cable.
    Eliminating the router/firewall/nat traversal will probaly work and maybe your router/firewall doesn't support nat traversal.
     
  20. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    I got another router, and it supports NAT, I can try it.

    EDIT: here's the connectiontest from the other router. http://s14.postimage.org/i654iqej5/Cattura_di_schermata_3.png

    EDIT2: Still, can't connect using external IP.
     
    Last edited: Jan 23, 2013
  21. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Ok, just to make sure, in the earlier screenshots I see that your trying to connect to 2550 but in the last ss the connection tester runs on 9999 ?
     
  22. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    it says the same thing using port 2550, still, it refuses to connect.
     
  23. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Ok and what if you don't use a router ?
     
  24. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    I have no clue about how to don't use a router.
     
  25. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Well you just swapped the router for another one, so you disconnected that one from the internet and connected the new one to it.
    Basicly the same but then with the computer and probably set the computer to DHCP so you can get the ip from the provider.
     
  26. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    that's what all I got for DHCP Options, should I disable it?

    http://s9.postimage.org/r62n86873/Cattura_di_schermata_4.png
     
  27. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    hmm no, that are the settings on the router.
    When you connect the computer directly to internet, you don't have to change things on your router.
    I do however advise you to be carefull, connecting a computer to the internet without a firewall isn't a good idea in general.
     
  28. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    I DO have a firewall, the first one is in the router, the other one is the default windows 8 firewall (for what I do, is good enough.)
     
  29. NSmith

    NSmith

    Joined:
    Jan 23, 2013
    Posts:
    3
    Try http://www.canyouseeme.org/ to check if your port is open. If it does not see the open port you know its a problem with your modem \ router \ firewall. Make sure the server is running when checking.

    I have a cable modem which has a router built in and a nice router I purchased. When setting up my server I went through a similar problem. Turned out the modem had the ports blocked. Plugged server into modem to bypass router and then unblocked those ports. I got it working but wasn't happy with the setup. I called cable company and they disabled DHCP on modem to let all traffic pass through. Now my router handles all traffic and port forwarding works correctly.

    As a test you can also try moving your PC into the DMZ on router. This exposes the pc directly to the internet, no port forwarding needed. I wouldn't recommend leaving your PC in the DMZ for long periods of time. Last test would be disable the windows firewall.

    Hopefully one of these things will help you.
     
    Last edited: Jan 23, 2013
  30. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67


    Maybe I wasn't prepared for this situation (In case you didn't noticed, i'm running windows 7 from External HDD, to check if windows 8 was the real problem, and it isn't.)
     
  31. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    So you have not opened the port in your firewall and port-forwarded correctly, as I was saying from the start.
     
  32. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    I disabled router, but still all the ports (except port 445) are disabled and even if I open them they remain disabled.
     
  33. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    I don't know guys, I tried tons of apps that require a portforwarded IP (Xampp, minecraft server, mysql etc.) and they all work, but Unity doesn't, at this point I think i'm going to quit unity, it's useless.

    EDIT: In fact, using XAMPP and activating Apache with a normal portforward (and a regular router) this is what happens.

    http://s14.postimage.org/n94qldach/Cattura_di_schermata_5.png

    but then, I try to use my server on the same port 80 (after I disabled XAMPP, obviously.)

    http://s2.postimage.org/bjs5ca1i1/Cattura_di_schermata_6.png

    so, I'm sure now that the ports are correctly forwarded and Unity hates me, if someone have a USEFUL hint, please use it and now, if anyone can't understand my issue, I guess i'll quit Unity.
     
    Last edited: Jan 24, 2013
  34. CazicThule

    CazicThule

    Joined:
    Nov 29, 2012
    Posts:
    95
    Run your server on an Amazon EC2 server with -nographics -batchmode and see if it works.
     
  35. uraniumbathtub5

    uraniumbathtub5

    Joined:
    Apr 7, 2012
    Posts:
    59
    This is an easy one to solve, You will not be able to connect to yourself through your external IP
    In easy money my game available at www.buttcrackstudios.com I programmed a custom master server and you will be able to do the same thing too if you rent a 3rd party server and try to connect to it, trying to connect to your local machine wont work.
     
  36. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    I tried everything with NO LUCK at all, if someone could provide me a windows free VPS (Or 7 days trial or whatsoever) it would be appreciated a lot.
     
  37. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    Well guys, this is my last post.
    I managed to solve this, by getting a trial VPS, and it seems to work (however, now my issue is that I don't know how to use the -batchmode at all)

    so this is a message to everyone who has got my same problem:

    IF YOU CAN'T CONNECT EVEN IF YOU OPENED THE DOOR, Forget it, really, FORGET IT, and get a VPS, it's the only way, for now.
     
  38. TwentySicks

    TwentySicks

    Joined:
    Sep 6, 2012
    Posts:
    51
    Seems like I've encounted the same problem as you, however, 14 days ago or so, It worked perfectly fine remotely.

    Today I had to show my work to my colleagues and guess what, it just doesnt work. Everything is forwarded, I've done everything you have done, but it just wont connect remotely.

    I have no clue why it worked before, and not now, because I havent changed anything network related. I've ONLY done a little GUI work, and now it cant connect.

    I dont know what is up lol.
     
  39. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    If you are using a VPS, I've met the same fate, my only way to get trough that was to enable Nat and disable the VPS Firewall.
     
  40. TwentySicks

    TwentySicks

    Joined:
    Sep 6, 2012
    Posts:
    51
    Might end up using a VPS, but I'm looking into using Photon instead, as we decided to not use direct connect and get a masterserver instead. Still, the Unity masterserver is a bit unreliable, so we are looking at Photon.
     
  41. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    It's a test server so yes it's unreliable, you can download the source code, compile it and run it yourself.
     
  42. TwentySicks

    TwentySicks

    Joined:
    Sep 6, 2012
    Posts:
    51
    I am talking about my own master server here, using their source.

    I still doesnt work very well, i'm afraid :( As in, even though everything is open and ready to be connected through, it still fails me sometimes. It just isnt stable for some reason.
     
  43. scippo97

    scippo97

    Joined:
    Jul 22, 2012
    Posts:
    67
    Not for me, it works painlessly and I got no problem connecting.
     
  44. twospoonstn

    twospoonstn

    Joined:
    Mar 10, 2013
    Posts:
    12
    I'm having pretty much exactly the same issue. All my networking code works fine locally. When try connecting to my server remotely it gives;

    System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it.

    at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP, Boolean requireSocketPolicy) [0x00000] in <filename unknown>:0
    at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <filename unknown>:0
    at System.Net.Sockets.TcpClient.Connect (System.Net.IPEndPoint remote_end_point) [0x00000] in <filename unknown>:0
    at System.Net.Sockets.TcpClient.Connect (System.Net.IPAddress[] ipAddresses, Int32 port) [0x00000] in <filename unknown>:0
    UnityEngine.Debug:Log(Object)

    Using these lines of code;
    var client = new TcpClient(host, port);

    Writing that same code in Visual Studio 2010 (win forms app) works fine. Telnet to the host and port works fine. NodeJs script can connect fine.

    I'm totally lost here to what is going on. This is NOT a firewall issue (since it works in every other programming language I have tried).
     
  45. twospoonstn

    twospoonstn

    Joined:
    Mar 10, 2013
    Posts:
    12
    A reboot fixed this for me. Not sure what was going on.
     
  46. Darren Wong

    Darren Wong

    Joined:
    Nov 14, 2012
    Posts:
    4
    hi ps3cfw,

    Can u please provide me the full code for master server and client server? i'm facing problem of client connecting with ip port.

    Thank you.
     
  47. Omidja

    Omidja

    Joined:
    Jul 16, 2014
    Posts:
    19
    i Solved it!But I dont know is it a Good way but its work!
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Connecter : MonoBehaviour {
    6.    
    7.         public HostData[]    hostData;
    8.     private float         clearTime;
    9.    
    10.     void Start() {
    11.         Screen.sleepTimeout = SleepTimeout.NeverSleep;      
    12.             MasterServer.ClearHostList();
    13.             MasterServer.RequestHostList( "yourFVName" );
    14.             hostData = MasterServer.PollHostList();
    15.     }
    16.    
    17.     IEnumerator DelayedConnection() {
    18.         yield return new WaitForSeconds( 0.0f );
    19.        
    20.         string connectionResult = "";
    21.             if( hostData.Length > 0 ) {
    22.                  connectionResult = "" + Network.Connect( hostData[ 0 ] );
    23.                 LANParty.log += "\nConnect to Server @ " + hostData[ 0 ].gameName + " " + hostData[ 0 ].gameType + " " + hostData[ 0 ].guid + " " + hostData[ 0 ].ip + " " + hostData[ 0 ].useNat + " " + connectionResult;
    24.             }
    25.             else
    26.                 connectionResult = "";
    27.  
    28.     }  
    29.    
    30.     void OnDisconnectedFromServer() {
    31.         DelayedConnection();  
    32.     }
    33.    
    34.     void Update() {
    35.         if( Input.GetKey( KeyCode.Escape ) )
    36.             Application.Quit();
    37.         if( (  Network.peerType == NetworkPeerType.Disconnected ) || ( Network.peerType != NetworkPeerType.Disconnected && Network.connections.Length < 1 ) )
    38.             return;
    39.         print (hostData[ 0 ].ip);
    40.     }
    41.    
    42.     void OnPlayerDisconnected( NetworkPlayer player ) {
    43.         Network.RemoveRPCs( player );
    44.         }
    45.  
    46.  
    47.     public void StartServer () {
    48.         Network.InitializeServer( 50, 2301, true );
    49.         MasterServer.RegisterHost( "yourFVName", "Test" );
    50.         foreach (GameObject GameobjectsInGame in FindObjectsOfType(typeof(GameObject)))
    51.         {
    52.             GameobjectsInGame.SendMessage("OnLoaded",SendMessageOptions.DontRequireReceiver);
    53.         }
    54.     }
    55.     public void ConnectServer () {
    56.         if( hostData.Length == 0 )
    57.             hostData = MasterServer.PollHostList();
    58.         StartCoroutine( "DelayedConnection" );
    59.     }
    60.  
    61.     }
    62.  
    any problem:Comeaniceday@gmail.com
     
  48. MFen

    MFen

    Joined:
    Sep 25, 2013
    Posts:
    13
    Isn't this just using Unity's Test MasterServer I don't see where you're changing the IP Address away from the default.
     
  49. Strom_CL

    Strom_CL

    Joined:
    Nov 17, 2012
    Posts:
    112
    Similar scenario here, turns out you can't connect to your own External IP address on Comcast. Everyone could connect to me fine, I just can't resolve to my own external IP. Weird eh?