0

Hi i develop chat gui with socket. I want to send text and photo message. Photo messages came to server but server don't send to clients. i wrote for send photo code inside '''. When I write server.py this code gui be very very slowly and dont send text messages and only one send photo to server.

Server codes

 from socket import *
from threading import *

clients=[]
names =[]

def clientThread(client):
    initname = client.recv(1024).decode('utf-8')
    bayrak = True
    while True:
        try:
            if bayrak:
                names.append(initname)
                print(initname, 'connect')
                bayrak = False

         '''   if client.recv(1024).decode('utf8') == "came to photo":
                filepath = client.recv(1024).decode('utf-8')
                filepath = filepath.split("/")[-1]
                file = open(filepath, "wb")
                image_chunk = client.recv(1024)

                while image_chunk:
                    file.write(image_chunk)
                    image_chunk = client.recv(1024)
                file.close()        '''

            message = client.recv(1024).decode('utf-8')


            for c in clients:
                if c != client:
                    index = clients.index(client)
                    name = names[index]
                    c.send((name + ':' + message).encode('utf-8'))

        except:
            index = clients.index(client)
            clients.remove(client)
            name = names[index]
            names.remove(name)
            print(name + 'out')
            break


server = socket(AF_INET, SOCK_STREAM)

ip= 'localhost'
port = 8081

server.bind((ip,port))
server.listen()
print('Server is listening')

while True:
    client, address = server.accept()
    clients.append(client)
    print('Connect', address[0]+ ':' + str(address[1]))
    thread = Thread(target=clientThread, args=(client,))
    thread.start()

Client Codes

client = socket(AF_INET, SOCK_STREAM)
ip= 'localhost'
port = 8081

client.connect((ip, port))

pencere = Tk()
pencere.title('Chat')


messages =Text(pencere, width=50)
messages.grid(row=0, column=0, padx=10, pady=10)
yourMessage = Entry(pencere, width=50)
yourMessage.insert(0, 'Name')
yourMessage.grid(row=1,column=0,padx=10,pady=10)
yourMessage.focus()
yourMessage.select_range(0, END)

def sendMessage():
    clientMessage = yourMessage.get()
    messages.insert(END, '\n'+ 'You: ' + clientMessage)
    client.send(clientMessage.encode('utf-8'))
    yourMessage.delete(0, END)


def openFile():
    client.send("came to photo".encode("utf-8"))
    filepath = fd.askopenfilename()
    client.send(filepath.encode("utf-8"))
    file = open(filepath,"rb")
    data = file.read(1024)

    while data:
        client.send(data)
        data = file.read(1024)
    file.close()

bmessageGonder = Button(pencere, text= 'Send',command=sendMessage)
bmessageGonder.grid(row=2,column=0,padx=5,pady=5)

dosyaSec = Button(pencere, text = 'Select Photo', command=openFile)
dosyaSec.grid(row=3,column=0,padx=5,pady=5)



def recvMessage():
    while True:
        serverMessage = client.recv(1024).decode('utf-8')
        messages.insert(END, '\n' + serverMessage)



recvThread = Thread(target=recvMessage)
recvThread.daemon= True
recvThread.start()

pencere.mainloop()
azoss
  • 39
  • 5
  • Do you understand how it should work? – user253751 Nov 29 '22 at 21:29
  • One client send photo to server and server send this photo all clients – azoss Nov 29 '22 at 21:31
  • and how should the program do this? – user253751 Nov 29 '22 at 21:33
  • socket architecture allows us to do this – azoss Nov 29 '22 at 21:37
  • yes. how? . . . – user253751 Nov 29 '22 at 21:43
  • we have one server and we have multi clients. Clients connect to server and interaction will start – azoss Nov 29 '22 at 21:45
  • Yes. You need to design the interaction. Any idea what the interaction will be like? – user253751 Nov 29 '22 at 21:45
  • What we call interaction is the client sending data to the server and the server sending a response to all clients, right? – azoss Nov 29 '22 at 21:50
  • There are two things wrong with `serverMessage = client.recv(1024).decode('utf-8')`. 1) sockets don't work that way, just because you send 1024 bytes in a single send doesn't mean the receiving side will read 1024 bytes in a single recv, see [this answer for more](https://stackoverflow.com/a/43420503/238704), 2) these are photos, not text, so why are you trying to decode the image bytes into strings using utf-8. – President James K. Polk Nov 29 '22 at 21:52
  • It is up to you. You have to design it. I want to make sure you understand that you have to figure out how the client and server work together - you cannot just randomly call send and recv with absolutely no idea what is going on. – user253751 Nov 29 '22 at 21:52
  • `from socket import *` and `from threading import *` will pollute your namespace with numerous names and likely conflict with variable and method names you're using, causing hard-to-diagnose bugs. – President James K. Polk Nov 29 '22 at 21:55

0 Answers0