Search Unity

NAT Error when swapping Server and Client

Discussion in 'Multiplayer' started by shaun, May 26, 2008.

  1. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    When I finish a NAT facilitated game and change the server to the client, I get this error;
    Code (csharp):
    1. NAT target 192.168.1.122:25002 not connected to NAT facilitator 83.221.146.11:50001
    The process is as follows:
    1. Node1 acts a server and initializes the game
    2. Node2 is the client and joins
    3. Play game and finish
    4. Node2 now starts a game, and acts as the server
    5. Node1 tries to join, but consistently fails with the above error message

    Am I not cleaning something up correctly?
     
  2. nafonso

    nafonso

    Joined:
    Aug 10, 2006
    Posts:
    377
    Aha! Another person with the same problem that I have!

    I can also replicate that same issue, and the only way to make the client connect again is to restart the game :cry:
     
  3. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Good, I was starting to think I was doing something totally stupid.

    I thought it might be because the facilitator is getting confused when a node registers as a server and then registers again as a client, since there is no way to unregister a NAT enabled game. The only thing here is that my games all have unique identifiers, so in theory it shouldn't be a problem - but obviously it is.

    EDIT: I've tried the MasterServer.UnregisterHost(), but still having the same issue.
     
  4. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    To fix this problem, you have to set UseNat=false before doing Network.Connect() on a client that has been a server with UseNat=true.

    I was setting my server by default to UseNat=true, which doesn't apply for clients, since the NAT punching is used to facilitate connections to the server not from the client.

    Hope this makes sense - and thanks to Larus for clarifying the UseNat property :)
     
  5. nafonso

    nafonso

    Joined:
    Aug 10, 2006
    Posts:
    377
    Thanks for sharing! :wink:

    Although, if NAT isn't used for clients, why doesn't the client simply ignore that variable (or force false instead of its value) when connecting to a server? I only set useNat when creating the server, when the user is connecting I simply ignore it, because I didn't think that it influenced how the connection was created. Again, another line that should be in the docs, although in my opinion Unity should force a false under the hood.

    And if we read the docs it says:
    Which doesn't really comply with what you wrote. Maybe the docs need some cleaning.

    Regards,
    Afonso
     
  6. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Well, I could be mistaken about UseNat not affecting clients. However if you consider the role of NAT Punchthru, it's really to allow outside computers to find a node on an internal network, which to me shouldn't affect a client, as it is connecting to a server, and not being connected to.
     
  7. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    Actually it affects both. Both have to connect to the Facilitator, which in our cases is the same as the Master Server OTEE provided. So the Client must not connect directly to the desired server, but through the Faciliator, initiating the whole Punchthrough.

    Thanks a lot for sharing though. This will definetely save some headaches ;)
     
  8. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Ok, so the UseNat is actually meaning connected a server using Nat, rather than use Nat myself. I think the ambiguity is in the naming, or doco.
     
  9. DocSWAB

    DocSWAB

    Joined:
    Aug 28, 2006
    Posts:
    615
    Just to clarify a bit:

    1. The server's Network.useNat should be set to whatever it is capable of. If it has a public IP, then it can be false. If it is behind a router, etc., AND it can do NAT punchthrough (as determined by the connection tester), then is should be set to true. If it is behind a router and can't do NAT punchthrough, then it can advertise itself to the Master Server, but nobody will be able to connect to it.

    2. The client's useNat should match the server it is trying to connect to. That will come through as part of the host information from the facilitator (Master Server). If the client has a public IP number or it can do NAT punchthrough, it will be able to connect to either public IP (NAT = false) or NAT-facilitated server games. If the client can't do NAT punchthrough, and it doesn't have a pubic IP, then it can't connect at all.

    So there's no one setting for the client's useNat setting. It's most likely that you saw this because the server the client was connecting to has a public IP, so the client couldn't connect with useNat = true.