diff --git a/src/server.py b/src/server.py --- a/src/server.py +++ b/src/server.py @@ -33,9 +33,12 @@ nodeStack=collections.deque([0]) incoming=networkReader.output # synchronized message queue outcoming=networkWriter.input +i1=-1 + while True: jsonData,binData=incoming.get(timeout=2) + dataFile=open(filename,mode="rb+") if jsonData["command"]=="init": assert jsonData["blockSize"]==localTree.BLOCK_SIZE @@ -52,11 +55,12 @@ while True: elif jsonData["command"]=="send" and jsonData["dataType"]=="data": # needlessly allow hashes and data in mixed order print("received data block #{0}: {1}...{2}".format(jsonData["index"],binData[:5],binData[-5:])) - - dataFile=open(filename,mode="rb+") - dataFile.seek(jsonData["index"]*localTree.BLOCK_SIZE) + + i2=jsonData["index"] + if i1+1!=i2: + dataFile.seek(i2*localTree.BLOCK_SIZE) dataFile.write(binData) - dataFile.close() + i1=i2 # never update the hash tree @@ -68,5 +72,6 @@ while True: # fr.close() # fw.close() +dataFile.close() conn.close() sys.exit(0)