What I am doing is sending the file name and the file size first, an encoded (utf-8) json string, so the server can know in advance the size of the file so he knows when all data have arrived.
It's working great when just one client is sending the file, but when 2 or more clients are sending at same time very often the server crashes (sometimes it works, what makes it more confusing) with 'utf-8' codec can't decode byte 0xff in position 36: invalid start byte, in the first line of the run()
fuction.
I don't have a clue why is this happening, because each client has his individual process, and shouldn't be having any conflict between them.
Client:
import socket, json
f = 'img.jpg'
f_bin = open(f, 'rb').read()
info = {'name': f, 'size': len(f_bin)}
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(('89.1.59.435', 9005))
s.send(json.dumps(info).encode())
total_sent = 0
while total_sent < info['size']:
try:
sent = s.send(f_bin[total_sent:])
except Exception as err:
break
total_sent += sent
Server:
import socket, threading, json
def get_file(conn, info):
remaining = info['size'] # file size, our trigger to know that all packages arrived
file_bin = b''
progress = None
while remaining > 0:
try:
package = conn.recv(1024)
except Exception as err:
return None
file_bin += package
remaining -= len(package)
return file_bin
def run(conn):
info = json.loads(conn.recv(1024).decode()) # 'utf-8' codec can't decode byte 0xff in position 36: invalid start byte
file_bin = get_file(conn, info)
if file_bin is not None:
dest = 'files/{}'.format(info['name'])
with open(dest, 'wb') as f:
f.write(file_bin)
print('success on receiving and saving {} for {}'.format(info['name'], conn.getpeername()))
conn.close()
host, port = ('', 9005)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host, port))
sock.listen(5)
while True:
conn, addr = sock.accept()
print('conn', addr)
threading.Thread(target=run, args=(conn,)).start()
I removed the prints just to post the relevant part and illustrate the problem