0

I'm currently creating an encrypted chat program. Text chat works well. However, I want to implement file transfer, but it doesn't work. My code is trying to work in a way that when one client tries to transfer a file, the server receives it and sends it to another client. When I type '/filetransfer' to transfer file.

Dick: hi
/filetransfer
Sending...
Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Python\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Python\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "c:\Users\USER\Desktop\filetest\client.py", line 198, in sndChat
    self.sndFile()
  File "c:\Users\USER\Desktop\filetest\client.py", line 233, in sndFile
    clientSocket.send(l)

This error occurred. I think the client cannot send the file data.

Also, I would like to apply Diffie-Hellman and AES used for text encryption to file transfer. I spent a lot of time here, but it doesn't work. I desperately need help...

Client.py

def rcvChat(self):
        
        print("\nWelcome to Chatee!") 
        
        while True:
            try:
                message = clientSocket.recv(4096).decode(encodeType)


                
                if self.thred_done:
                    message=self.aes.decrypt(message)
                    print(message)    
                    if message == 'filetransfer start':
                        filereceive_thread = threading.Thread(target=self.rcvChat)
                        filereceive_thread.join()

                        #write_thread = threading.Thread(target=self.sndChat)
                        #write_thread.join()
                        #self.rcvFile()
                        break
def sndChat(self):

        while True:
            message = input('')
            if message == '/filetransfer':
                message = self.aes.encrypt(message)
              
                clientSocket.send(message) 
                writefile_thread = threading.Thread(target=self.sndChat)
                writefile_thread.start()
           

                self.sndFile()       
                break     
           
                              
            message = self.aes.encrypt(message)
              
            clientSocket.send(message)
            
 def sndFile(self):
        print("---File Transfer---")
        print("Type a file name...")

        filename = 'C:\\Users\\USER\\Desktop\\filetest\\test.txt'
        #clientSocket.send(filename.encode(encodeType))
        #data_transferred = 0

        if not exists(filename):
            print("The file doesn't exsit.")

        f = open(filename,'rb')
        print ('Sending...')
        l = f.read(8096)
        while (l):
            print ('Sending...')
            #data_transferred += clientSocket.send(l)
            clientSocket.send(l)
            l = f.read(8096)
        f.close()
        print ('Done Sending')
        #clientSocket.shutdown(socket.SHUT_WR)
        print (clientSocket.recv(8096))
        #clientSocket.close    
 def rcvFile(self):

        #filename = clientSocket.recv(1024).decode(encodeType)
        #filename = self.aes.decrypt(filename)
        filename = 'received.txt'
        f = open(filename,'wb')

            
        while True:
            
            print ('Receiving...')
            l = clientSocket.recv(8096)

            if not l:
                print("Fail file transfer")
                #sys.exit()

            while (l):
                print ('Receiving...')
                f.write(l)
                l = clientSocket.recv(8096)
            f.close()
            print ('Done Receiving')
            

Server.py

def handle_client(self,client,client_addr):

        client_pvt_key=self.client_keys[client]
        client_name=self.clients[client]
        

        print(f"[{client_addr[0]}]-{client_addr[1]} - [{client_name}] - Connected")
        print(f"Active Connections - {threading.active_count()-1}")
      
        self.broadcast(f'{client_name} has joined the chat!\n\n')
      
        aes=AESCipher(client_pvt_key)

        while True:
            try:
                msg = aes.decrypt(client.recv(self.header)) #복호화 안하고 바로 브로드캐스트 해도 될듯
                
                if msg == '/filetransfer':
                    #보낸 사람 제외하고 보내기
                    self.broadcast('filetransfer start')
                    thread = threading.Thread(target=self.sndFile, args=(client, ))
                    thread.start()
                    thread.join()
                               
                    
                
                elif msg==self.quit_msg:
                    break
                print(f"[{client_addr[0]}]-{client_addr[1]} - [{client_name}]")

                msg=f'{client_name}: {msg}'
                self.broadcast(msg)

            except:
                break
        
        client.close()
        print(f"[{client_addr[0]}]-{client_addr[1]} - [{client_name}] - quit_msged")
        del self.clients[client]
        del self.client_keys[client]
       
        self.broadcast(f'{client_name} has left the chat\n')
        print(f"Active Connections - {threading.active_count()-2}")
def broadcast(self,msg):
        for client in self.clients:
            aes=AESCipher(self.client_keys[client])
            crypted_msg=aes.encrypt(msg)
            client.send(crypted_msg)
def sndFile(self, client):
        print("---File Transfer---")
        #print("Type a file name...")

        client_pvt_key=self.client_keys[client]

        aes=AESCipher(client_pvt_key)

        #filename = client.recv(1024).decode(self.encodetype)
        #self.broadcast('fuck',filename)
        while True:
            try: 
                l = client.recv(8096)
                print('Rceiving...')
                
                #del self.clients[client]
                for client in self.clients:
                
                    client.send(l)

            #client.send(filename.encode(self.encodetype))
            #l = client.recv(8096)
                if not l:
                    print("Fail file transfer")

                            
            except:
                print('file fail')
                break
배문성
  • 1
  • 1
  • If you can't provide a better description of what goes wrong than "it doesn't work", how would we even begin to look for the problem? – jasonharper Oct 15 '21 at 02:47
  • 2
    As a side note: if you start a thread and join it immediately, you don't need the thread. – Klaus D. Oct 15 '21 at 02:59
  • *"I would like to apply Diffie-Hellman and AES used for text encryption to file transfer. I spent a lot of time here, but it doesn't work."* - if you want transport encryption why not just use TLS? Anyway, "doesn't work" neither describes what you've actually tried and what kind of problem you phase. The question is basically a large dump of mostly undocumented code with a short info what you want to implement and then a "doesn't work". This kind of information does not make a useful question. – Steffen Ullrich Oct 15 '21 at 06:01
  • https://stackoverflow.com/a/43420503/238704 – President James K. Polk Oct 15 '21 at 23:12
  • @배문성 - How do you run the shown client methods? – Armali Oct 16 '21 at 16:20

0 Answers0