Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
May199 committed Apr 21, 2021
1 parent 0dfd455 commit adfec24
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
5 changes: 3 additions & 2 deletions UDP/Client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import time


class Client():

def __init__(self, address = 'localhost', port = 3000):
Expand Down Expand Up @@ -50,13 +51,13 @@ def header():
pk = file.read(packet_bytes)
server.sendall(pk)

sent = f' {int((i+1)*packet_kilobytes)} | '
sent = f'\n {i}:({int((i+1)*packet_kilobytes)})KB'

print(sent, end='')

time.sleep(delay)

print('\nmessage sent to: ', self.address, self.port)
print('\n message sent to: ', self.address, self.port)

header()

Expand Down
20 changes: 11 additions & 9 deletions UDP/Server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,28 @@ def connect(self):
files = []

while True:
data, addressConection = server.recvfrom(1024)

conn, addr = server.recvfrom(1024)

if data.decode() == 'closed':
if conn.decode() == 'closed':
break

filename = data.decode()
filename = conn.decode()
print(f'[{len(files)}] {filename}')
files.append(filename)

# Escolhendo arquivo que vou querer baixar do cliente
fileselect = int(input('\n Which file do you want to receive?'))

while not (0 <= fileselect < len(files)):
print('invalid option!')
fileselect = int(input('\n Which file do you want to receive?'))

server.sendto(fileselect.to_bytes(4, 'little'), addressConection)
server.sendto(fileselect.to_bytes(4, 'little'), addr)
# Recebendo numero de pacotes
# Queremos saber em quantos pacotes o arquivo sera enviado
data = server.recv(4)
packet = int.from_bytes(data, 'little')
conn = server.recv(4)
packet = int.from_bytes(conn, 'little')

# Recebendo pacotes
server.settimeout(5)
Expand All @@ -53,16 +55,16 @@ def connect(self):

start = time.time()
for i in range(packet):
data = server.recv(packet_bytes)
file.write(data)
conn = server.recv(packet_bytes)
file.write(conn)

time_download = f'Downloading ... {round((100*(i+1))/packet, 2)}%'
print('\r'+time_download, end='')

total_time = round(time.time()-start, 2)
print(f'\nDownload complete: {total_time} milliseconds')

print('message received from: ', str(addressConection))
print('message received from: ', str(addr))

server.close()

Expand Down

0 comments on commit adfec24

Please sign in to comment.