IP ADDRESSES AND LOCALHOST
An IP (internet protocol) address identifies a device on the internet or on a local area network (LAN) if an internet connection exists. An IP address can be identified by four numbers separated by three full-stops. For example a valid IP address could be 172.168.1.212. A local host address is always referred to as 127.0.0.1 and is also known as the loopback address.
In Multiplayer Demonstration mode, which this tutorial is based on, only local host addresses are used in both instances so both use 127.0.0.1 with different port numbers so that the communications don’t clash on the same device. When the Multiplayer Demonstration mode isn’t activated, the IP address of the devices is used and each instance has to run on a separate device for the multiplayer feature of the game to work. You’ll notice that there are some multiplayer settings defined in games.py that refer to IP addresses. self.clientAddressPort = ([‘127.0.0.1’, 20002]) self.serverAddressPort = ([‘127.0.0.1’, 20001]) self.clientIPAddress = self. clientAddressPort[0] self.serverIPAddress = self. serverAddressPort[0] self.clientPort = 20002 self.serverPort = 20001 self.multiplayerDemo = False
By default, both client and server IP addresses are set up as the localhost address (127.0.0.1) because at this stage in the program there’s nothing to identify otherwise. An address port variable is also set up for both server and client that contains both the IP address and port number being used.
The port numbers in this example are used to receive communications. Note that 20001 is to receive data on the server port and port number 20002 is to receive data on the client port.