'Server is receiving data even after connection closed

I'm sending a file to the client. It works great. But when i'm sending a big file and close the client's terminal suddenly(which is sending data at that time), server doesn't stop writing the data.(After I close the connection(terminal of the client), more time I wait, more server’s file get bigger.)I want to make system protected against error because i may encounter this problem. The still-working part (in server)is:

while True:
                data = conn.recv(4096)
                if not data:
                    break
                f.write(data)

server.py:

import time
import socket
port = 3030                  
s = socket.socket()             
host = '' #public ip(aws)
s.bind((host, port))           
s.listen(5) 
print ('Server listening....')

def resultf(supp):
    return 'calculated'    

while True:
    try: 
        conn, addr = s.accept()   
        print ('Got connection from', addr)        
        supp = conn.recv(1024)
        print('Server received', supp.decode('utf-8'))        
        conn.send(supp)
        
        with open('tobecalculated.txt', 'wb') as f:            
            
            while True:
                data = conn.recv(4096)
                if not data:
                    break
                f.write(data)           

        conn.close()

        result=resultf(int(supp))        
        conn, addr = s.accept()        
        conn.send(str(result).encode())        
        print('Done sending')

        
    except Exception as E:
        print(E)
        try:
            conn.send(b"Exception occurred. Try again")
        except Exception as SendError:
            pass
        
    finally:
        conn.close()

client.py:

import socket  
import time      

try:
    s = socket.socket()    
    host=''   #  Server public ip
    port =3030               
    s.connect((host, port))    
    supp=1    
    s.send(str(supp).encode("utf-8"))    
    print(s.recv(1024).decode())      
    
    with open('tobecalculated.txt', 'rb') as f:
        l = f.read()
        while (l):
            s.send(l)
            l = f.read()
      
    s.shutdown(socket.SHUT_WR)

    s.close()
        
    while True:
        try:
            s = socket.socket()
            s.connect((host, port))
            print('Receiving')
            result = s.recv(1024)
            print('Received:')
            break
        except Exception as calc:
            print('Calculating... Please wait',calc)
 
    with open('file.txt', 'wb') as f:        
        f.write(result)       
    
    print(result.decode())
    s.close()
    print('Connection closed')
    
except Exception as E:
    print(E)

Lastly, I tried(after closing connection/terminal) this to see if data is same but it says 'not same'

       
import time
import socket
port = 3030                  
s = socket.socket()             
host = '' #public ip(aws)
s.bind((host, port))           
s.listen(5) 
print ('Server listening....')

def resultf(supp):
    return 'calculated'    

while True:
    try: 
        conn, addr = s.accept()   
        print ('Got connection from', addr)        
        supp = conn.recv(1024)
        print('Server received', supp.decode('utf-8'))        
        conn.send(supp)
        global a
        a=0
        index=0
        with open('tobecalculated.txt', 'wb') as f:            
            
            while True:
       
                data = conn.recv(4096)
                if data==a:                    
                    print('same',index)
                else:
                    print('not same',index))
                index+=1
                a=data
                if not data:
                    break
                f.write(data)
        conn.close()

        result=resultf(int(supp))        
        conn, addr = s.accept()        
        conn.send(str(result).encode())        
        print('Done sending')

        
    except Exception as E:
        print(E)
        try:
            conn.send(b"Exception occurred. Try again")
        except Exception as SendError:
            pass
        
    finally:
        conn.close()
    


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source