KareemRj
KareemRj

Reputation: 79

Not sending full file size always -1

This code is supposed to be a p2p file sharing python code. It sends the file, but it doesn't send the full size. I don't know what is wrong that it is doing that. First I encountered decoding errors and searched it and got the encoder. Other than that the file was not showing and it was fixed. Now the file is showing but it isn't sending the full size. When I try opening the sent file I get "corrupted" or "wrong file type" this is server.py

import os
import socket
import time
import math

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

sock.bind((socket.gethostname(), 12000))
sock.listen(10)
print("Host Name: ", sock.getsockname())

client, addr = sock.accept()
print("Connected to : ", addr)

file_name = input("File Name:")
file_size = os.path.getsize(file_name)
print("File Size: ", file_size)
client.send(file_name.encode("ISO-8859-1"))
client.send(str(file_size).encode("ISO-8859-1"))

with open(file_name, "rb") as file:
    c = 0

    start_time = time.time()

    while c <= file_size:
        ChunkAnnouncer = math.ceil(math.ceil(c) / 5)
        print("Chunk Size: ", ChunkAnnouncer)

        data = file.read(4096)
        if not (data):
            break
        client.sendall(data)
        c += len(data)

    end_time = time.time()

print("File Transfer Complete.Total time: ", end_time - start_time)
sock.close()

client.py

import socket
import time
import os

host = input("Host Name: ")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    sock.connect((host, 12000))
    print("Connection Successful")
    print("Connected to: ",(host, 12000))
except:
    print("Connection Failed")
    exit(0)

file_name = sock.recv(100).decode()
file_size = sock.recv(100).decode()
print(file_name)
start_time = time.time()
sock.send(file_name.encode())
confirmation = sock.recv(1024)
encoding="ISO-8859-1"
if confirmation.decode(encoding) == "file-doesn't-exist":
    print("File Doesn't Exist")
else:
 write_name = 'from_server '+file_name
 if os.path.exists(write_name) : os.remove(write_name)
with open(write_name, 'wb') as file :
        while 1:
            data = sock.recv(4096)
            if not data:
                break
            file.write(data)
            end_time = time.time()
print(file_name, ' Downloaded Successfully')
print("Elapsed Time: " ,end_time - start_time)

I honestly can't see the error. if you can help..

Upvotes: 0

Views: 72

Answers (1)

po.pe
po.pe

Reputation: 1162

I adapted the client.py as follows and it worked

import socket
import time
import os

host = input("Host Name: ")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    sock.connect((host, 12000))
    print("Connection Successful")
    print("Connected to: ",(host, 12000))
except:
    print("Connection Failed")
    exit(0)

file_name = sock.recv(100).decode()
file_size = sock.recv(100).decode()
print(file_name)
start_time = time.time()
# sock.send(file_name.encode())
# confirmation = sock.recv(1024)
# encoding="ISO-8859-1"
# if confirmation.decode(encoding) == "file-doesn't-exist":
#     print("File Doesn't Exist")
# else:
write_name = 'from_server '+file_name
if os.path.exists(write_name):
    os.remove(write_name)
with open(write_name, 'wb') as file :
        while 1:
            data = sock.recv(4096)
            if not data:
                break
            file.write(data)
            end_time = time.time()
print(file_name, ' Downloaded Successfully')
print("Elapsed Time: " ,end_time - start_time)

The thing is that your server doesn't treat the response from client

# sock.send(file_name.encode())

and also doesn't send the confirmation.

Upvotes: 0

Related Questions