- Home /
Get host IP and port on client
On a client app, can I get the host's IP and port (and if hosting, it'll return your own IP and port) from code? I want to display them on each player's screen to make it easier to connect to games by direct IP. The point of this would be for 2 friends to play together with #1 hosting and #2 connecting by direct IP. #1 gets their IP and port and sends it to #2
#2 copies this info into direct connect (123.456.678:123)
Anybody know how to do this? Thanks!
Answer by sacredgeometry · Feb 13, 2020 at 07:51 AM
How can you get a host IP unless you know what the host is? If I am understanding you what you are saying it is basically:
"Can I get the address of a parcel I want to send without knowing the address"
The answer is no.
If the hosts are aggregated on a server somewhere then yes of course you can. You just need to send a request to the server for the information and it needs to send it back to you, then you just display it. At that point a named connection will probably be more user friendly than an ip and port.
But if its p2p then no. They would have to be connected to a central server and you would have to be able to differentiate them somehow. Either that or know how to address them personally.
If you are asking, can I get my public facing ip to send to someone... then yes you can do that. There are plenty of services you can use to do that.
Here are a few: https://stackoverflow.com/questions/3253701/get-public-external-ip-address/45242105
Answer by edthered1009 · Feb 14, 2020 at 01:03 AM
I believe it is the third option. As the host, I want to be able to get the IP and port of the application and display it onscreen. When a client isn't connected, they can't see any IP or port because they aren't on a server. If they are connected, it'll show the host's port and IP.
Thanks!