-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.py
94 lines (66 loc) · 1.86 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
import socket
import time
import sys
import random
from multiprocessing import *
bufSize = 4096
class MainClient:
def __init__(self):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.size = ["50", "200", "500", "1000"]
def connect(self, host, port):
try:
self.socket.connect((host, port))
except Exception as e:
print(e)
return False
def sendVideoRequest(self):
i = random.randrange(0, 1)
self.requestSize = self.size[i]
print("Request " + self.requestSize)
self.socket.send(self.requestSize.encode())
def recvVideoResponse(self):
#recvFile = open("recvFile.mp4", "wb")
startTime = time.time()
length = self.socket.recv(bufSize).decode()
remain = int(length[:length.index('e')]) - (len(length) - length.index('e') + 1)
while remain > 0:
buf = self.socket.recv(bufSize)
remain -= len(buf)
endTime = time.time()
interval = str(endTime - startTime)
print("Success " + self.requestSize + " " + interval) # seconds
self.socket.send(("t" + interval).encode())
self.socket.close()
def Simulation1_handle(host, port, wait):
client = MainClient()
print(host, port, '로 연결')
client.connect(host, port)
client.sendVideoRequest()
client.recvVideoResponse()
def Simulation1():
try:
num_clients = 10
host, port = '127.0.0.1', 9090
processList = []
for i in range(num_clients):
process = Process(target=Simulation1_handle, args=(host, port, 0))
process.daemon = True
process.start()
processList.append(process)
for i in range(num_clients):
processList[i].join()
except KeyboardInterrupt:
print("Ctrl C - Stopping server")
finally:
for process in active_children():
process.terminate()
process.join()
print("Close server")
sys.exit(1)
if __name__ == '__main__':
#try:
Simulation1()
#except KeyboardInterrupt:
# print("Ctrl C - Stopping server")
# sys.exit(1)