-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.py
More file actions
137 lines (124 loc) · 3.96 KB
/
Client.py
File metadata and controls
137 lines (124 loc) · 3.96 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from socket import *
import threading
from random import randint
from time import *
BUFF_SIZE = 10240
filename = ""
servername = gethostname()
serverport = 12000
UDP_Port = randint(12001 , 20000)
clientsocket = socket(AF_INET , SOCK_STREAM)
clientsocket.connect((servername , serverport))
clientUDPsocket = socket(AF_INET , SOCK_DGRAM )
clientUDPsocket.bind((servername, UDP_Port))
def listen(Mysocket):
global BUFF_SIZE
global filename
# print "we are in listen area !"
while True:
counter1 = 0
servermessage = Mysocket.recv(1024)
print "From Server : " , servermessage
if(servermessage[0:10] == "NewStream#"):
filename = servermessage[10:]
print "new Stream Recieved !"
print "Do You Want this file ? "
elif(servermessage[0:10] == "StreamReq#"):
print servermessage
elif(servermessage[0:7] == "Stream#"):
# print "Im here in the listen function elif !"
Next_UDP_Port = int(servermessage[7:])
# print "Next port is ", Next_UDP_Port
# print "filename is : " , filename
myFile = open(filename , 'rb')
# message = myFile.read(BUFF_SIZE)
while(1):
# print "sending " , message
nowtime = time()
message=myFile.read(BUFF_SIZE)
# print "message is :" , message
if not message:
print "In the reading if !"
break
# if(counter1 == 0):
# nowtime = time()
# else:
# counter1 += 1
# print "first time is" , nowtime
clientUDPsocket.sendto(message,(servername , Next_UDP_Port))
thentime = time()
diff = thentime - nowtime
# print "thentime is " , thentime
# print "diff is " , diff
if(diff != 0):
print "Upload rate is : " , BUFF_SIZE/(diff * 100000) , "Mbps"
print "Got out of while"
clientUDPsocket.sendto("##&&##" ,(servername , Next_UDP_Port))
myFile.close()
# print "message is :" , message
# print "sent!"
# choice = raw_input("Do you want the file ?")
# print "this is my choice :" , choice
# def talk(Mysocket , option = "Enter your Command : "):
# print "option is " , option
# print "we are in talking area !"
# while True:
# Command = raw_input(option)
# Mysocket.send(Command)
def UDPfunc():
global filename
counter = 0
while True:
# print "Thread running"
modifiedmessage , serveraddress = clientUDPsocket.recvfrom(BUFF_SIZE)
# print "in udp listening ... :" , modifiedmessage , "-----------",modifiedmessage[0]
# filename_got = str(UDP_Port)+filename
gotfile = open(filename , 'wb')
# print "before while"
while(1):
# print "in the while"
# print "recieved message :" , modifiedmessage
# if(counter == 0):
# time1 = time()
# else:
# counter += 1
time1 = time()
# print "time1 is : " , time1
if(modifiedmessage[0] == "##&&##"):
print "got in the if"
break
gotfile.write(modifiedmessage[0])
modifiedmessage = clientUDPsocket.recvfrom(BUFF_SIZE)
time2 = time()
# print "time 2 is : " , time2
# print "Download Rate is : , time 1 : " , time1 , " time2 is :" , time2 , "difference is :" , time2 - time1
diff = time2 - time1
# print "diff is :" , diff
if(diff != 0):
print "Upload rate is : " , BUFF_SIZE/(diff * 100000) , "Mbps"
print "Out of Recieved while!"
# gotfile.write(modifiedmessage[0])
gotfile.close()
print "File is now closed!"
clientsocket.send("GOT#")
threading.Thread(target=listen , args=(clientsocket,)).start()
threading.Thread(target=UDPfunc , args=()).start()
while True:
# print "Enter your Command : "
Command = raw_input()
if(Command[0:4] == "Reg#"):
clientsocket.send(Command)
elif(Command[0:3] == "Bye"):
clientsocket.send(Command)
elif(Command[0:10] == "StreamReq#"):
filename = Command[10:]
clientsocket.send(Command)
elif(Command[0:4] == "YEAH"):
print "UDP port is :" , UDP_Port
# print clientUDPsocket.gethostname()
newCommand = Command + "#" + str(UDP_Port)
clientsocket.send(newCommand)
elif(Command[0:4] == "NOPE"):
clientsocket.send(Command)
#threading.Thread(target=talk , args=(clientsocket,)).start()
# clientsocket.close()