2
\$\begingroup\$

Unity 2018.2.0 makes the Network class obsolete. I have used "Network.player.ipAddress" in my code to get the local LAN IP address.

    internalIP = Network.player.ipAddress;
    externalIP = new WebClient().DownloadString("http://icanhazip.com");

What code should replace this? What would be best practice for getting internal and external IP addresses for manual LAN and internet direct connections between server and client?

Internal/external IP addresses are needed so players can load the game, then tell their friends what their IP is to direct connect.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ When you tell us why exactly you need the internal and external IP address, then we might suggest you a better solution for your problem. \$\endgroup\$
    – Philipp
    Jul 15, 2018 at 15:31
  • \$\begingroup\$ @Philipp So players can load the game and tell their friends what their IP is for direct connecting. \$\endgroup\$ Jul 15, 2018 at 16:58
  • 1
    \$\begingroup\$ Have you considered setting up a matchmaking server? It would be a very lightweight task, so a small and cheap cloud VM would do. \$\endgroup\$
    – Philipp
    Jul 15, 2018 at 18:12
  • \$\begingroup\$ @Philipp That might be a good idea in future, but I really need an answer to this specific implementation now. \$\endgroup\$ Jul 15, 2018 at 19:50

1 Answer 1

2
\$\begingroup\$

This will output the IP for every interface on the machine.

foreach(NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
   if(ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
   {
       foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
       {
           if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
           {
               //do what you want with the IP here... add it to a list, just get the first and break out. Whatever.
               Debug.Log(ip.Address.ToString());
           }
       }
   }  
}

You'll need to have access to System.Net.NetworkInformation in your project.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks! This works to bring up a list of IP addresses. The problem is the first one it returns is 192.168.0.10. That is pulled on both editor and standalone client. Previously using Network.player.ipAddress, whenever I'd load a standalone and the editor, and try and direct connect them, they'd have a different 192.168.xxx. So I'm wondering, is there something I can do manually to fix this, or is it something coded deeper into Unity which is now removed and I can't replicate? \$\endgroup\$ Jul 16, 2018 at 18:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .