-2

im setting up a local network game however when i attempt to send a packet size for unpicking a dictionary i keep getting None returned

heres my server (stripped down to the not working portion)


player1 = { # players 2-4 are the exact name but renamed
    "id":1,
    "x": 50,
    "y": 50,
    "alive": True,
    "joined": False
}
def client(con, player_data):
    global player1, player2, player3, player4
    player = player_data
    print(player)

    all_players = (player1, player2, player3, player4)
    msgsize = str(sys.getsizeof(all_players))
    print(msgsize.encode())
    con.send(msgsize.encode())

ootput of the prints

{'id': 1, 'x': 50, 'y': 50, 'alive': True, 'joined': False}
72

my client

class Network:
    def __init__(self, port):
        self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server = "100.115.92.197"
        self.port = port
        self.addr = (self.server, self.port)
        
    def connect(self):
        self.client.connect(self.addr)

    def recv(self):
        message_size = self.client.recv(1000).decode()
        print(message_size)

clients output

None

ive tried struct and sys now

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
  • 1
    How are you invoking the client? Show that code too. It's in there somewhere. For example, if you do `net = Network(...) ... print(net.recv())` then it's going to print what the `recv` method returns, which is `None`. – kindall Jun 16 '22 at 21:34
  • Make a [mcve]. That means the *exact* code to produce the problem when copied and run with **no changes**. – Mark Tolonen Jun 16 '22 at 23:02
  • 1
    Also, `sys.getsizeof()` is not the function to get the size of the player list. That returns the memory size of the Python object itself. – Mark Tolonen Jun 16 '22 at 23:05
  • im trying to return the memory footprint so i know how much to accept, the client is being initiated in a second file with the connect command being run on initiation – Karsonthefoxx Jun 17 '22 at 01:16
  • 1
    The "memory footprint" of a python object is unrelated to the bytes sent. compute the bytes to send and call `len()` on it. `sys.getsizeof(b'x')` (a single byte) returns 34. `len(b'x')` returns 1. – Mark Tolonen Jun 17 '22 at 05:19
  • thats not my problem in the first place, for some reason its not sending the value period, but thanks for the heads up – Karsonthefoxx Jun 19 '22 at 04:26
  • And which part of the code is supposed to send the value? I don't see it. – gre_gor Jun 19 '22 at 09:01
  • its the `con.send` at the end of the first block – Karsonthefoxx Jun 19 '22 at 22:35

1 Answers1

0

i was doing con.send instead of con.sendall, i solved my issue.