-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
113 lines (91 loc) · 3.61 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from file import *
from packet import Packet
import const
import multiprocessing
import os
import socket
import struct
import time
def get_my_ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
return ip
def receive_thread(file_name, client_address, thread):
receiver_port = thread.get()
print('acquiring port: {}, client: {}'.format(receiver_port, client_address))
receiver_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
receiver_socket.bind((get_my_ip(), receiver_port))
# send our port
port_message = receiver_port.to_bytes(2, byteorder='little')
receiver_socket.sendto(port_message, client_address)
# opening to be written file
destination_path = get_destination_directory()
full_path = destination_path+'/'+file_name
if (os.path.exists(full_path)):
os.remove(full_path)
destination_file = open(full_path, 'ab')
counter = 0
# receiving data
delayed_checksum = None
latest_seq_num = -1
not_matching = 0
while True:
counter += 1
packet = receiver_socket.recv(35000)
valid, packet_data = Packet.read_packet_from_bytes_array(bytearray(packet))
# verify packet
if not(valid):
continue
seq_num = packet_data[const.INDEX_SEQNUM]
if (seq_num != (latest_seq_num + 1)):
not_matching +=1
print('seq not matched!', seq_num, latest_seq_num)
if not_matching > 2:
if (packet_data[const.INDEX_TYPEVAR] == FIN):
receiver_socket.sendto(const.FINACK.to_bytes(1, byteorder='little'), client_address)
else:
receiver_socket.sendto(const.ACK.to_bytes(1, byteorder='little'), client_address)
continue
# if (delayed_checksum):
# print(str(delayed_checksum)+' '+str(packet_data[const.INDEX_CHECKSUM]))
# delayed_checksum = None
# send ack
file_chuck = packet_data[const.INDEX_DATA]
# if ((counter % 150) == 0):
# delayed_checksum = packet_data[const.INDEX_CHECKSUM]
# print('delayed')
# time.sleep(2)
# continue
destination_file.write(file_chuck)
not_matching = 0
latest_seq_num = seq_num
# print('---------')
if (packet_data[const.INDEX_TYPEVAR] == FIN):
receiver_socket.sendto(const.FINACK.to_bytes(1, byteorder='little'), client_address)
destination_file.close()
print('File: "{}" has finished transfer'.format(file_name))
break
else:
receiver_socket.sendto(const.ACK.to_bytes(1, byteorder='little'), client_address)
def main():
SERVER_PORT = int(input("Enter server port: "))
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind((get_my_ip(), SERVER_PORT))
print("UDP server up and listening")
print("Server IP: {} | PORT: {}".format(get_my_ip(), SERVER_PORT))
pool = multiprocessing.Pool(processes = 100)
pool_manager = multiprocessing.Manager()
port_queue = pool_manager.Queue()
portList = range(5001, 6000)
for port in portList:
port_queue.put(port)
while True:
file_name, client_address = server_socket.recvfrom(const.THIRTYTWO_KB)
file_name = file_name.decode('utf-8')
print('Receiving file: {}'.format(file_name))
# create new receiver process
new_receiver_process = pool.apply_async(receive_thread, (file_name, client_address, port_queue))
if __name__ == '__main__':
main()