0

i'm making an online version of MS PAINT in python, and i'm having trouble when i'm trying to transfer the X and Y of the brush to the server.instead of going one by one, its messy

instead of going one by one, its messy

that's how i send:

def paint(self, event):
    self.line_width = self.choose_size_button.get()
    paint_color = 'white' if self.eraser_on else self.color
    if self.old_x and self.old_y:
        self.c.create_line(self.old_x, self.old_y, event.x, event.y,
                           width=self.line_width, fill=paint_color,
                           capstyle=ROUND, smooth=TRUE, splinesteps=36)
    self.old_x = event.x
    self.old_y = event.y
    data = (str(self.old_x) + "," + str(self.old_y) + "," + paint_color + "," + str(self.line_width))
    my_socket.send(data.encode())

and that's how i recieve in the server:

for sock in rlist:
    if sock is listening_socket:
        client_socket, addr = listening_socket.accept()
        open_sockets.append(client_socket)
        print ("opensockets=", open_sockets)
    else:
        data = sock.recv(1024).decode()
        print(data)
        if data == "quit":
            quit_sock(sock)
        else:
            send_xy(data, sock)
Nir Cohen
  • 11
  • 1
  • if you will take a look on the photo i uploaded("instead of going one by one, its messy") you will see that when i paint slowly, it's sending the cordinates one by one and it's all good. but when i paint faster, the cordinates mix with each other and that's making a big mess. that's my problem. thanks for your answer – Nir Cohen Feb 02 '22 at 10:23
  • 1
    You make the mistake that seemingly every new python socket programmer makes: you assume that whatever data you send in a single `send()` call will be received in a single `recv()` call. Please read [this answer](https://stackoverflow.com/a/43420503/238704) for a better understanding. You will need a proper protocol with message boundaries to do what you want. Also, do not use `send()` unless you're checking the return value and prepared to retry. Use `sendall()` instead. – President James K. Polk Feb 02 '22 at 13:41
  • Thank you very much :) – Nir Cohen Feb 03 '22 at 17:42

0 Answers0