actnmk
actnmk

Reputation: 166

C socket Client communicate with Server outside LAN without knowing IP address

I write a small game using socket in which multiple clients join the game, everything works in the LAN. Now I want to extend to clients outside LAN, what I wonder is: if I don't know the IP address of the client then will the program work?

Inside LAN I use:

char *ip = "127.0.0.1";

Upvotes: 0

Views: 344

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595392

The client needs to know the server's IP in order to connect to it.

If the client and server run on the same machine, the server can listen on 127.0.0.1 and the client can connect to that IP.

If the client and server run on the same LAN, the server needs to listen on the NIC that is connected to the LAN, and the client can connect to the server's LAN IP.

If the client is running on a different network and wants to connect to the server over the Internet, the client must connect to the server's public Internet IP, as assigned by the server's ISP. And if the server is running behind a NAT router, the public IP is assigned to the router and not the server directly, so the router must have port forwarding configured to route incoming connections to the server machine on the router's LAN.

If the client doesn't know the IP to connect to, the server will need to publish its IP somewhere that the client can query it, such as via DNS, a website, a central public server that all clients connect to for relaying, etc.

Upvotes: 2

Related Questions