How to get IP address on C# in program which developing for android device in Unity 2018.2+?
Seems like Network.player.ipAddress
is deprecated right now, so I'm looking for a new way.
How to get IP address on C# in program which developing for android device in Unity 2018.2+?
Seems like Network.player.ipAddress
is deprecated right now, so I'm looking for a new way.
FINALLY resolved by Unity (from about version 2018.2)
Inside any
[Command]
arriving on the server,the IP of the client, which, sent the
[Command]
isPhew.
You will in fact have your own
NetworkBehaviour
derived class, you must do this. (It is very often called "Comms"):That class must override both the server/client startoffs, so that each Comms "knows what it is".
On any actual project, since Unity doesn't do it, the clients must identify themselves to the server. You have to entirely handle that in Unity networking, from scratch.
(Note. that process is explained in the long blog here
https://forum.unity.com/threads/networkmanager-error-server-client-disconnect-error-1.439245/#post-3754939 )
So this would be the first thing you (must) do in
OnStartLocalPlayer
That "command pair" inside comms would look like this:
You must do this in all networking projects (or you will have no clue which client is which):
The good news is Unity have added the
connectionToClient
property, that is to say on the server, inside a[Command]
.And then, you can indeed use the new
.address
property on that.YOU CAN FINALLY GET THE IP OF THAT CLIENT!
It's that "simple", the ip of the client, which just connected, is "THEDAMNEDIP".
Phew.
Only took Unity 5? years.
The
Network.player.ipAddress
has been deprecated since it is based on the old obsolete Unity networking system.If you are using Unity's new uNet Network System, you can use
NetworkManager.networkAddress
;If you are using raw networking protocol and APIs like TCP/UDP, you have to use the
NetworkInterface
API to find the IP Address. I useIPManager
which has been working for me on both desktop and mobile devices:IPv4:
IPv6:
The
IPManager
class:For 2018...
However, SEE THE NEWER ANSWER BELOW.
It does seem to be the case that you can now call - on the server, in NetworkManager - simply
NetworkConnection#address
and it will give it to you.Recall that you must write your own NetworkManager, it does not work out of the box. Explained here if yer new to unity networking:
https://forum.unity.com/threads/networkmanager-error-server-client-disconnect-error-1.439245/#post-3754939
.
However note that
1) this only gives it to you on the server,
2) and only at "NetworkManager" which (of course!) is not actually when you identify each connecting client
Note the even newer answer - Unity finally resolved this.
this is a minor modification to the excellent answer already provided by @Programmer. the motivation here is to improve functionality on MacOS and iOS. i haven't looked at android yet. i've also only tested IPV4.
what's different from @Programmer's original:
and another to simply return the last one, which matches the logic of the original version.
en0
,en1
). This option should obviously not be used if all you want is the IP address itself.