0

Have some trouble with python's sockets. I have a problem with sending images. When I sending one image from server to client it is all right, but when I tried to send more than one image it looks like, that it even haven't been recieved and nothing happened. Problem part in recieve method. When I tried to send more than one image my programm haven't even go 'try' in my recieve method. My code is next:

import socket
from tkinter import *
import tkinter.scrolledtext as scrolledtext
import threading
from tkinter import filedialog as fd

HOST = '192.168.1.2' #my ip
PORT = 9090

class Client:
    def init(self, host, port):

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect((host, port))

        self.gui_done = False
        self.running = True

        gui_thread = threading.Thread(target=self.gui_loop)
        receive_thread = threading.Thread(target=self.receive)

        gui_thread.start()
        receive_thread.start()

    def gui_loop(self):
        self.win = Tk()
        self.win.title('Client 2')
        self.win.configure(bg="lightgray")

        self.chat_label = Label(self.win, text="Chat:", bg="lightgray")
        self.chat_label.config(font=("Arial", 12))
        self.chat_label.pack(padx=20, pady=5)

        self.text_area = scrolledtext.ScrolledText(self.win)
        self.text_area.pack(padx=20, pady=5)
        self.text_area.config(state='disabled')

        self.msg_label = Label(self.win, text="Message:", bg="lightgray")
        self.msg_label.config(font=("Arial", 12))
        self.msg_label.pack(padx=20, pady=5)

        self.input_area = Text(self.win, height=5)
        self.input_area.pack(padx=20, pady=5)

        self.send_button = Button(self.win, text="Send", command=self.write)
        self.send_button.config(font=("Arial", 12))
        self.send_button.pack(padx=20, pady=5)

        self.gui_done = True

        self.win.protocol("WM_DELETE_WINDOW", self.stop)
        self.win.mainloop()

    def write(self):
        file = open(fd.askopenfilename(), 'rb');
        data = file.read(2048)

        while data:
            self.sock.send(data)
            data = file.read(2048)

        file.close()

    def stop(self):
        self.running = False
        self.win.destroy()
        self.sock.close()
        exit(0)

    def receive(self):
        while self.running:
            try:
                chunk = self.sock.recv(2048)
                if self.gui_done and chunk:
                    file = open('lol.jpg', 'wb')
                    while chunk:
                        file.write(chunk)
                        chunk = self.sock.recv(2048)

                    file.close()
                    chunk = None
##                    client_socket.close()
            except ConnectionAbortedError:
                print("Hey!")
                break
            except:
                print("error")
                self.sock.close()
                break

client = Client(HOST, PORT)
Vladimir
  • 47
  • 1
  • 6
  • 2
    In short: TCP is just a byte stream. If you want to use the same TCP connection to send multiple messages (images) you need to define and implement some higher level application protocol which defines where a message ends and the next one starts. – Steffen Ullrich Sep 11 '21 at 15:30

0 Answers0