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.