0
\$\begingroup\$

I have two scripts, a server script and a client script. I built two parts of the app, one with the client script and another one with the server script. I put both apps on devices, connected two devices with the local network and run the server. First I wrote this line in the client to get the IP of the server device:

NetworkManager.singleton.networkAddress

But it gives me the IP of the client device. Do you have any solution to this problem?

Thanks.

\$\endgroup\$
1
  • \$\begingroup\$ I'm not sure but perhaps the NetworkDiscovery component is what you're looking after. I'm not sure how it's used, though, as I'm not an expert with Unity. \$\endgroup\$
    – Vaillancourt
    Commented Aug 6, 2018 at 17:14

2 Answers 2

1
\$\begingroup\$

You cannot simply get the server's IP from the client. There is no way to do that without using some kind of service discovery system.

That is why we have server browsers in most games. They provides a list of IPs of servers to the clients with some info about them. Now how do the server browser have the IPs of all the running servers ? Well, servers register themselves to the server browser when they start. The server browser being accessible by a domain name or a fixed IP.

Server discovery system in a local area network :

  • The server listens to a specific port (say 25000) for incoming UDP packets.
  • The clients broadcasts a UDP packet to port 25000 asking for servers addresses.
  • Every server that receive that UDP packet will answer with their own IP address (though the IP address can be retrieved in the packet header) and some additionnal information if necessary.
  • The client then receives the IP of all available servers on the same LAN.
  • If the client has not received any answers after a certain amount of time (60 sec for example) it concludes no server is available on the LAN.

This solution has flaws :

  • UPD is not reliable (but you can build a reliable system on top of UDP)
  • It only works if everybody is on the same LAN
  • It only works if UDP broadcast is possible (some networks may disable broadcasts)
  • It is not secure (man in the middle, packet forgery, etc...)

Best luck with your project.

\$\endgroup\$
4
0
\$\begingroup\$

The normal way to find servers is via DNS. Servers have well-known names such as "gamedev.stackexchange.com", and by the process of name lookup you find the an IPv4 or IPv6 address for them.

If you need more than a single server, there are a few options. There are DNS-based solutions (google.com isn't a single server, you get varying IP addresses when looking it up in DNS). Alternatively, you can have a single top-level server that keeps track of the "player lobby" and redirects gamers to game servers with spare capacity.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks sir for your replying, all I need to make client get the ip address of server device in local network no more \$\endgroup\$ Commented Aug 6, 2018 at 13:34

You must log in to answer this question.

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