Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Pr0n · Jul 12, 2015 at 01:54 PM · networkingunity multiplayerip

Local IP adress in UNet

Hello, I need to get local IP address. Is it possible?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
16
Best Answer

Answer by Pr0n · Jul 12, 2015 at 01:51 PM

I already solved my question with this method:

 public string LocalIPAddress()
     {
         IPHostEntry host;
         string localIP = "";
         host = Dns.GetHostEntry(Dns.GetHostName());
         foreach (IPAddress ip in host.AddressList)
         {
             if (ip.AddressFamily == AddressFamily.InterNetwork)
             {
                 localIP = ip.ToString();
                 break;
             }
         }
         return localIP;
     }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image -chris · Mar 25, 2016 at 08:50 AM 1
Share

Worth noting that using System.Net; and using System.Net.Sockets; are required.

avatar image YuHeLong · May 15, 2018 at 09:27 AM 0
Share

That doest gets me my localIp which is start with something like 192.168.x.x, this code return me my actual IP address

avatar image Bunny83 YuHeLong · May 15, 2018 at 10:38 AM 0
Share

A single computer can have multiple IP addresses. That's why IPHostEntry has an AddressList. He just picks the first IP address. Also there is no "actual" IP address. You maybe meant the external IP address? Also since there are IPv4 and IPv6 addresses you may want to inspect each IPAddress more closely

avatar image
6

Answer by mgsteinkamp · Oct 29, 2015 at 10:13 AM

 return Network.player.ipAddress;

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image softrare · Jan 25, 2016 at 11:21 AM 2
Share

This is the legacy way of doing it. It may still work now, because it reads the IP address of the network interface of the OS but this call could be removed in the future.

avatar image $$anonymous$$ softrare · Jun 22, 2018 at 10:44 AM 0
Share

with 2018.2 it is

avatar image
4

Answer by softrare · Jan 25, 2016 at 11:18 AM

Even easier ;)

 NetworkManager.singleton.networkAddress
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ivan-santiago · Feb 02, 2016 at 08:10 AM 2
Share

But this is not the own IP, this is the network IP. For the client, this returns the IP of the server. I think the answer of "mgsteinkamp" is the correct one. Furthermore, according to the documentation, Network.player is not legacy yet.

avatar image $$anonymous$$ ivan-santiago · Jun 27, 2018 at 05:18 AM 0
Share

Except that it's now a legacy network call and removed with 2018.2

avatar image
2

Answer by Stephen_O · Jul 22, 2018 at 05:54 PM

In 2018.2 Network.player.ipAddress is no longer available.

I've found this and from a few tests it seems to work:

 string hostName = System.Net.Dns.GetHostName();
 string localIP = System.Net.Dns.GetHostEntry(hostName).AddressList[0].ToString();

Edited: now using GetHostEntry instead of GetHostByName because it's obsolete warning CS0618.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jul 22, 2018 at 09:34 PM 2
Share

So found the same solution Pr0n posted 3 years ago a few posts above which is the accepted answer and has 12 upvotes? Looks like a redundant answer to me.


Also note that blindly picking the address 0 is just a recipe for disaster. For some reasons a client might not have an IP address at all or, as i mentioned above, he could have several addresses. Some might be IPv4 some IPv6


Just as an example this are the addresses i get on my PC:

 // #0  Family: InterNetworkV6   addr: fe80::a499:48ee:40c0:82cf
 // #1  Family: InterNetworkV6   addr: 2003:ec:73c6:1d00:45be:b326:ccc3:ecc6
 // #2  Family: InterNetworkV6   addr: 2003:ec:73c6:1d00:a399:48ef:40c0:82cf
 // #3  Family: InterNetworkV6   addr: fe80::94ba:a400:acf4:f58d
 // #4  Family: InterNetworkV6   addr: fe80::4c36:13ad:edd:190d
 // #5  Family: InterNetworkV6   addr: fe80::30d5:1e26:3f57:4deb
 // #6  Family: InterNetwork     addr: 192.168.178.20
 // #7  Family: InterNetwork     addr: 192.168.16.1
 // #8  Family: InterNetwork     addr: 192.168.135.1
 // #9  Family: InterNetworkV6   addr: 2001:0:9d38:78cf:30d5:1e26:3f57:4deb
  

I have several virtual adapters for virtual machines and VPN. As you can see my first address is not an IPv4 address (note that some values has been changed for safety ^^)

avatar image malkere · Nov 16, 2018 at 12:34 PM 0
Share

This does indeed return an IPv6 in my case. Anyway to deter$$anonymous$$e through the list who is correct?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[UNET] Is there an alternative for MasterServer.RequestHostList in UNET? 1 Answer

Ping an Ip 1 Answer

[UNet] "No such host is known" error on iOS 1 Answer

Servers and ip address 1 Answer

Strange error?! - Invalid IL - IL_001c: pop 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges