Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to find server's IP from client?

Discussion in 'Multiplayer' started by bigkahuna, Aug 28, 2008.

  1. bigkahuna


    Apr 30, 2006
    I'm experimenting with the "Network Examples" project and am using the "Third Person" scene. What I want to end up with is a dedicated "server" and a dedicated "client" application and automate the network connection process as much as I can. There will only be two computers in this network (one server and one client) and they will either connect directly Peer to Peer or through a router. I'm trying to modify "ConnectGUI.js" for this (script follows).

    What I'd like to know is how do I find the "server's" IP from the client application (ie. I'd like to be able to get the variable "remoteIP" automatically rather than having to look it up and entering it in manually)?

    Code (csharp):
    1. //DontDestroyOnLoad(this);
    2. var remoteIP = "";
    3. var remotePort = 25001;
    4. var listenPort = 25000;
    5. var useNAT = false;
    7. function Awake()
    8. {
    9.     if (FindObjectOfType(ConnectGuiMasterServer))
    10.         this.enabled = false;
    11. }
    13. function OnGUI ()
    14. {
    15.     GUILayout.Space(10);
    17.     GUILayout.BeginHorizontal();
    18.     GUILayout.Space(10);
    19.     if (Network.peerType == NetworkPeerType.Disconnected)
    20.     {
    21.         GUILayout.BeginVertical();
    22.         if (GUILayout.Button ("Connect"))
    23.         {
    24.             Network.useNat = useNAT;
    25.             Network.Connect(remoteIP, remotePort);
    26.         }
    27.         if (GUILayout.Button ("Start Server"))
    28.         {
    29.             Network.useNat = useNAT;
    30.             Network.InitializeServer(32, listenPort);
    31.             // Notify our objects that the level and the network is ready
    32.             for (var go in FindObjectsOfType(GameObject))
    33.                 go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);    
    34.         }
    35.         GUILayout.EndVertical();
    36.         remoteIP = GUILayout.TextField(remoteIP, GUILayout.MinWidth(100));
    37.         remotePort = parseInt(GUILayout.TextField(remotePort.ToString()));
    38.     }
    39.     else
    40.     {
    41.         if (GUILayout.Button ("Disconnect"))
    42.         {
    43.             Network.Disconnect(200);
    44.         }
    45.     }
    47.     GUILayout.FlexibleSpace();
    48.     GUILayout.EndHorizontal();
    49. }
    51. function OnConnectedToServer() {
    52.     // Notify our objects that the level and the network is ready
    53.     for (var go in FindObjectsOfType(GameObject))
    54.         go.SendMessage("OnNetworkLoadedLevel", SendMessageOptions.DontRequireReceiver);    
    55. }
    57. function OnDisconnectedFromServer () {
    58.     if (this.enabled != false)
    59.         Application.LoadLevel(Application.loadedLevel);
    60.     else
    61.         FindObjectOfType(NetworkLevelLoad).OnDisconnectedFromServer();
    62. }
  2. Timmer


    Jul 28, 2008
    If I'm understanding your question correctly then: you can't.

    If you have a static server then you can hardcode it in (preferably using a name not an IP in case that changes). But, otherwise there's no magic to say "look up someone's IP". You either need to know the IP or the Domain Name of the server.
  3. bigkahuna


    Apr 30, 2006
    Hmm... So how would I "hardcode" a "name" in instead of the IP and Port in the above example? I would like to simplify this as much as possible, the "server" stand alone application will always run on one computer and the "client" stand along will always run on a different computer. There will only be one client. So the server application will never change, but the computer it runs on and the network will change. The "loader" scene example works fine if I'm running through a router and the internet. But when I set up a simple peer to peer network it doesn't work. I'd just hate to have to explain to folks how to find their IP address, it would be so much easier if it worked automagically...
  4. ProtonOne


    Mar 8, 2008
    So if I understand:
    - Single client
    - Single server
    - client and server are on the same local network
    - there could be multiple installations of this on multiple networks
    - could there be more than one server on the same local network?

    Closest I can imagine is that you could use the masterserver.

    The server would register itself like normal, then the client could poll for a list of all available servers.

    Go through the list and compare the HostData.ip to the local ip. The first 3 octets should match. If they do, connect. If not, wait 5 seconds and re-fetch a list (takes a while for the server to register itself).
  5. bigkahuna


    Apr 30, 2006
    So if I understand:
    - Single client
    - Single server
    - client and server are on the same local network
    - there could be multiple installations of this on multiple networks
    - could there be more than one server on the same local network?
    ... it will always be just two computers connected by a LAN, which could either be a Peer to Peer network (as in two Macs talking through an AirPort connection) or two PC connected to a router.

    I'm guessing you haven't looked at the "Networking Example Project" ( )? That's what I'm working with to learn this. That project has a "loader" example scene that essentially does what you're describing, but unfortunately that only works when the two computer are networked through a router and connected to the internet. When they are connected P2P it doesn't work.
  6. AmazingRuss


    May 25, 2008
    If its going to be on a lan, you could look into using UDP broadcast...which I'm guessing is available from mono.

    When your app starts, you have it send broadcast packets that say "I want to play" and listen for those packets from other clients. When you get one, it will have the IP address attached to it, and you can go from there.

    Sorry to be vague...I've done something similar in C++ but am noobish at C# yet.
  7. Corpano


    Feb 5, 2017
    I know, I am writing here 9 years after you guys :)
    Just to say, of course you can do multiple servers on one network :) Just make sure that port is different in them, so it will work.