简体   繁体   中英

Sockets error when different device is connected to the server

I was making a game and decided to see if I could connect to the server that was running on my machine on a different machine. I compiled the code on my machine (with pyinstaller) and then tried running it on the other machine. I worked fine at first but about 30 seconds later, the server stopped sending data to the other machine. I looked in the logs and saw Error client_thread: invalid literal for int() with base 10: b' 69, ' . I think this means that the other machine is partially sending data, rather than all of the data. This is the client code used to send data:

ge = {'position update': [player.id, player.x, player.y, player.flip, player.name, player.been_hit],"mini-update":[hit_player,player.score,start_game]}
data = json.dumps(ge)
try:
    self.socket.sendall((f"{len(data):<{HEADER_SIZE}}"+data).encode())
except socket.error as e:
    print(e)

and this is the code for receiving data:

data = ""
recv_data = conn.recv(bufsize)
if recv_data == b'':
    print(f"{player_id} DISCONNECTED")
    break
message_length = int(recv_data[:HEADER_SIZE])
data += recv_data.decode()
while True:
    if len(data)-HEADER_SIZE >= message_length:
        if len(data)-HEADER_SIZE > message_length:
            conn.setblocking(0)
            while True:
                try:
                    extra_data = conn.recv(bufsize)
                    if not extra_data:
                        break
                except:
                    break
            conn.setblocking(1)
        break
    recv_data = conn.recv(bufsize)
    data += recv_data.decode()
data = data[HEADER_SIZE:message_length+HEADER_SIZE]

Both of the machines are using Debian 10. Does anyone know why this might be happening?

Add .decode() function to the following lines:

recv_data = conn.recv(bufsize).decode()

...

extra_data = conn.recv(bufsize).decode()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM