2

so i made a game on pygame which is to be played over a LAN. it works fine when i run the server and i create client instances on my own machine. and i even used auto-to-py-exe to make it an executable so i can test it on other computers. The executable worked perfectly fine on my machine as well. But when i would send it and test it on another machine, the program doesnt even try to connect so none of the game information is sent, i dont know if i packaged the exe wrong or something, any help?

server code:

import socket
import pickle
from _thread import *
from game import Game
from player import Player


hostname = socket. gethostname()
local_ip = socket. gethostbyname(hostname)
print(local_ip)

#server = local_ip
port = 5555

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

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

s.listen()
print("Waiting for connection")
idCount = 0

def threaded_client(conn, p, gameId):
    #threaded client stuff
    


while True:
    #accept incoming connections
    conn, addr = s.accept()
    print("Connected to ", addr)

    idCount += 1
    p = 0
    gameId = (idCount -1)//2
    if idCount %2 == 1:
        games[gameId] = Game(gameId)
        print("making game ", gameId)
    else:
        p = 1
        games[gameId].ready()
        

    
    start_new_thread(threaded_client, (conn, p, gameId))

client code:


import pygame
from drawingTools import Tools
from game import Game
from player import Player
from player import Bullet
from sound import Sound
from network import Network


#dimensions
WIDTH = 900
HEIGHT = 500

HIT = pygame.USEREVENT + 1
EXPIRE = pygame.USEREVENT + 2

#create object
t = Tools(WIDTH, HEIGHT)
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game")





def main():
    #game loop
    run = True
    n = Network()
    s = Sound()
    #this is the method that should be connecting to the server and is where i believe the problem 
    is
    #everything else is game stuff, so i dont think i need to include it  
    n.connect()
    p1:Player = n.send("playerinnit")
    
    
    try:
            game: Game = n.send("get")
            
    except:
        print("error")
    
    
    clock = pygame.time.Clock()
    
    print("player ", p1.p)
    ship, r = p1.loadSprite(p1.p)
    
    
    
    s.music()

    while run:
        game stuff logic

network code:

import socket
import pickle

class Network:
    def __init__(self):
        
        self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server = "ip of the machine where the server is"
        self.port = 5555
        self.addr = (self.server, self.port)
        


    def getPlayer(self):
        return None
    #method that should be doing the connecting, works standalone or with a script but not the exe
    def connect(self):
        try:
            self.client.connect(self.addr)
            #return pickle.load(self.client.recv(4090))
        except:
            print("Could not connect")

    def send(self, data):
        try:
            self.client.send(str.encode(data))
            return pickle.loads(self.client.recv(4090))
        except socket.error as e:
            print(e)

    def sendObj(self, data):
        try:
            self.client.send(pickle.dumps(data))
            return pickle.loads(self.client.recv(4090))
        except socket.error as e:
            return []

    def disconnect(self):
        self.client.close()

main file that is now an exe:

from client import main, playAgain
if __name__ == "__main__":
    main()
    playAgain()

i dont know if yall need anything else like everything the auto-to-py-exe packaged, but any help would be appreciated!

Matt
  • 21
  • 2
  • There's all sorts of reasons why this isn't working on a remote system. What is the operating system on either systems? – ewokx Feb 24 '22 at 00:45
  • my computer is running windows 10 and the other machine is running windows 11 – Matt Feb 24 '22 at 00:58
  • I think the first thing to check is whether or not your port 5555 is opened in the firewall settings (on the system that is running the server code). – ewokx Feb 24 '22 at 02:04
  • 1
    in server you can use `0.0.0.0` instead of `socket.gethostbyname(hostname)` because sometimes it may give `127.0.0.1` and it doesn't give access from other computers – furas Feb 24 '22 at 02:58
  • so i created a separate client file on the second computer to test if its a common thing, and it connects just fine to the server. its only when i run the main exe that it doesnt connect. it throws a Nonetype error since it was supposed to recieve data from the server, but its passing through the connect method but not really connecting. – Matt Feb 24 '22 at 03:47
  • the only idea - debug it. You could use `print()` to display values in variables and which part of code is executed. And maybe run code without `try/except` to see full error messages. It may help to see what is the problem. You could also use other tools to check if there is connection between two computers on this port. ie. [netcat](https://en.wikipedia.org/wiki/Netcat), [nmap](https://en.wikipedia.org/wiki/Nmap) or even standard (on many systems) `telnet`. – furas Feb 24 '22 at 11:40

0 Answers0