-
Notifications
You must be signed in to change notification settings - Fork 0
/
recv_v1DEBUG vim.py
83 lines (73 loc) · 2.43 KB
/
recv_v1DEBUG vim.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
from pprint import pprint
from pydoc import cli
import socket
import time
import _thread
import os
from traceback import print_list
PORT = 5507
REMOTE = '10.0.0.2'
LOCAL = '10.0.0.1'
BUFFER_SIZE = 4096
PACKET_DATA_SIZE = 900
WINDOW_SIZE = 1
TIMEOUT = 1
count = 0
class custom_packet :
def __init__(self ,seq_number , syn , fin ):
self.seq_number = seq_number
self.syn = syn
self.fin = fin
def get_string(self):
temp = ("seq:"+str(self.seq_number)+":syn:"+str(self.syn)+":fin:"+str(self.fin)).encode()
return temp
def get_size(self):
temp = ("seq:"+str(self.seq_number)+":syn:"+str(self.syn)+":fin:"+str(self.fin)).encode()
return len(temp)
#stores all the recieved packets's data
recieved_data = dict()
base = 0
file = open("rcv1", "wb")
socket_recv = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
socket_recv.bind((LOCAL, PORT))
socket_send = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
def recv():
global recieved_data , base , REMOTE , PORT , count
while True:
rcvd_pckt , _ = socket_recv.recvfrom(BUFFER_SIZE)
message = rcvd_pckt.decode("latin-1").split(':')
# print(message)
rcv_seq = int(message[1])
rcv_syn = int(message[5])
rcv_fin = int(message[7])
begin_data = len(message[0]) + len(message[1]) + len(message[2]) + len(message[3]) + len(message[4]) + len(message[5]) + len(message[6]) + len(message[7]) + len(message[8]) + 9
data_rcv = rcvd_pckt[begin_data:]
if rcv_syn == 1:
print("Connection establishing")
base = rcv_seq
pk1 = custom_packet(rcv_seq , 1 , 0 )
socket_send.sendto(pk1.get_string() , (REMOTE ,PORT ))
elif rcv_fin == 1:
print("connection ending")
pk2 = custom_packet(rcv_seq , 0 , 1)
socket_send.sendto(pk2.get_string() , (REMOTE ,PORT ))
return
else:
# print("data_recieved")
pk3 = custom_packet(rcv_seq , 0, 0)
socket_send.sendto(pk3.get_string() , (REMOTE, PORT ))
if (rcv_seq - base) not in recieved_data.keys() :
count += 1
recieved_data[rcv_seq - base] = data_rcv
# print(data_rcv)
def write_file():
global file
for i in sorted(recieved_data) :
file.write(recieved_data[i])
return
def main_fn():
recv()
print(count)
write_file()
return
main_fn()