-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
115 lines (90 loc) · 2.6 KB
/
client.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
114
115
"""
Programmer: Luis Fernado Javier Velazquez Sosa
Course: CCOM 4205 - Computer Networks
Project: Transport layer - TCP
Discription:
This program will send a packege to a server
that will act as a tcp connection. This client
will send the sequence number, checksum and
payload length and the payload it self.
"""
from struct import *
from socket import *
import sys
#print(sys.argv[1])
#when final the user need to input the file it wants
if len(sys.argv) < 2:
print("You need to insert the file to send")
file = sys.argv[1]
#static variables
ADDRESS = "136.145.181.51"
PORT = 4206
serverAddrPort = (ADDRESS,PORT)
bufferSize = 4096
debug = 0
#Calculate checksum
def checksum(sq,msgLen,bMsg):
chksum = sq + msgLen + bMsg
return chksum
#Pack the package to send to the server
def packP(ack,checksum,msgLen,message):
p1 = pack('B',ack)#send ack
p2 = pack('I',checksum)#checksum
p3 = pack('H',msgLen)#message length
p4 = message.encode()#message encode
#print("sending", ack,checksum,msgLen,message)
pf = p1 + p2 + p3 + p4
return pf
#Byte representation
def byteR(i):
msgB = bytes(i,'ascii')
msgBsum = sum(msgB)
lenMsg = len(i)
return msgBsum,lenMsg
s = socket(AF_INET,SOCK_DGRAM)
#Initializing sequence number and cheaksum
seq = 0
chksum = 0
#opening file
f = open(file,'r')
rfile = f.readlines()
#print(rfile)
for i in rfile:
#sum of the byte representation and length
msgBsum ,lenMsg = byteR(i)
chksum = checksum(seq,lenMsg,msgBsum)
ack = 0
#print("Packing:",seq,chksum,lenMsg,i)
pack2send = packP(seq,chksum,lenMsg,i)
if debug:
print("Elements to calculate checksum:",seq,lenMsg,msgBsum)
print("Checksum =",chksum)
print("Package to send:",pack2send)
#print(pack2send)
while True:
s.sendto(pack2send,serverAddrPort)
print("Sending package",seq)
try:
s.settimeout(2)
x = s.recv(bufferSize)
except:
print("Timeout")
s.sendto(pack2send,serverAddrPort)
break
#unpack the message that is an acknowlage
ack = unpack("B",x)[0]
if ack == seq+1:
print("ACK:",ack)
seq += 1
break
#added this to make the code funtion
elif ack > seq+1:
seq += 1
print("ack",ack)
else:
print("Ack",ack)
seq = ack
continue
print("-"*75)
print("\nThanks for using the UDP+ service. Reliably sending your message. Thanks!\n")
print("-"*75)