1

I have a very basic server for my Pygame game going, but I don't know if/how I can make the server accessible from clients connecting from different networks. Currently I can connect from the same network, but because I'm using my local IP address for the socket, surely this won't work for connecting from different networks. Here are my four scripts (two of which are just classes):

File server.py

from _thread import *
import pickle
from player import Player

class Server:
    def __init__(self):
        self.players = [Player(0,0,20,20, (255,0,0)), Player(100,100,20,20,(0,0,255))]
        self.numPlayers = 0

        ip = (socket.gethostbyname(socket.gethostname()))
        print (ip)
        port = 5555

        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        try:
            self.s.bind((ip, port))
        except socket.error as e:
            print(e)

    def client_thread(self, conn, player):
        conn.send(pickle.dumps(self.players[player]))
        while True:
            try:
                data = pickle.loads(conn.recv(2048))

                if isinstance(data, Player):
                    self.players[player] = data

                playersToSend = []
                for each in range (0,len(self.players)):
                    if each != player:
                        playersToSend.append(self.players[each])

                conn.send(pickle.dumps(playersToSend))

                if data == "quit":
                    break

                if not data:
                    print ("Disconnected")
                    break
            except EOFError as e:
                break
        print ("Lost Connection")
        self.numPlayers -= 1


serv = Server()

serv.s.listen(20)
print("Waiting for connection, server started")

while True:
    conn, addr = serv.s.accept()
    print (f"Connection found from {addr}")
    start_new_thread(serv.client_thread, (conn,serv.numPlayers))
    serv.numPlayers += 1

File client.py


import pygame
from pygame import QUIT

from Network import Network

pygame.init()
win = pygame.display.set_mode((500,500))
n = Network() # initiate the network object
playerPos = (n.p.x, n.p.y) # connect to the server and get the starting position

print (playerPos[0],playerPos[1])

FPS = pygame.time.Clock()

def move():
    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]: n.p.y -= n.p.velocity
    if keys[pygame.K_DOWN]: n.p.y += n.p.velocity
    if keys[pygame.K_RIGHT]: n.p.x += n.p.velocity
    if keys[pygame.K_LEFT]: n.p.x -= n.p.velocity
    n.p.update()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            n.send("quit")
            pygame.quit()
            exit(0)

    
    move()

    players = [] # reset/set the list
    players = n.send(n.p) #send off the player to the server, to receiver the other player
    
    win.fill((255,255,255))

    for each in players:
        pygame.draw.rect(win, (255,0,0), pygame.Rect(each.rect))
    pygame.draw.rect(win, (0,0,255), pygame.Rect(n.p.rect))

    
    pygame.display.update()
    FPS.tick(60)

File network.py

import pickle


class Network:
    def __init__(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.ip = socket.gethostbyname(socket.gethostname())
        self.port = 5555
        self.addr = (self.ip, self.port)
        self.p = self.connect()

    def connect(self):
        self.sock.connect(self.addr)
        p = pickle.loads(self.sock.recv(2048))
        return p

    def send(self, data):
        self.sock.sendall(pickle.dumps(data))
        return pickle.loads(self.sock.recv(2048))

File player.py

import pickle


class Player():
    def __init__(self, x, y, width, height, colour):
        self.x, self.y = x, y
        self.width, self.height = width, height
        self.colour = colour
        self.velocity = 3

        self.rect = (x, y, width, height)

    def update(self):
        self.rect = (self.x, self.y, self.width, self.height)

    def draw(self, win):
        pygame.draw.rect(win, self.colour, self.rect)
Tom
  • 11
  • 2

1 Answers1

0

To connect to your server from other networks, you would need to set up your internet router to forward requests from the internet to your machine. This would be done by opening the configuration page on the router's ip address (just open http://<your_routers_ip> in a browser).
Unless your computer is online all the time, though, it would be better to use something like pythonanywhere to host your game, as it is a hosting service for python webapps.

  • I've had a quick look around, and it seems people mostly use python anywhere for websites. Is it applicable for game servers, if so are there any good tutorials for learning how to use pythonanywhere for that? – Tom Jul 06 '22 at 09:15
  • @Tom, I agree, that seems to be the case, but I don't see any reason why it wouldn't work for a game server. [This](https://stackoverflow.com/questions/26480008/using-pythonanywhere-as-a-game-server) question might help. – Riley Martin Jul 06 '22 at 13:10