-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
107 lines (90 loc) · 2.84 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
import numpy
import socket
import cv2
import threading
myip = socket.gethostbyname(socket.gethostname())
threadsnd = list(range(100))
vidsock = list(range(100))
sendsock = list(range(100))
threadrec = list(range(100))
threadcreate = list(range(100))
vidadd = list(range(100))
rec = list(range(100))
encoded = numpy.zeros((200, 200, 3))
shut = list(range(100))
count = 0
conn = list(range(100))
decimg = list(range(100))
final = cv2.imread("background_for_4.png")
def receive(sock, i):
global stop
global shut
global rec
global decimg
try:
while True:
rec[i] = sock.recv(190456)
arr = numpy.fromstring(rec[i], numpy.uint8)
decimg[i] = cv2.imdecode(arr, cv2.IMREAD_COLOR)
except socket.timeout:
stop = True
sock.close()
except ConnectionResetError:
print("encountered Connection reset error")
shut[i] = True
conn.close()
except cv2.error:
receive(conn, i)
def create_img(i):
global decimg
global final
while (True):
try:
if i == 0:
final[:150, :150] = decimg[i]
if i == 1:
final[:150, 150:300] = decimg[i]
if i == 2:
final[150:300, :150] = decimg[i]
if i == 3:
final[150:300, 150:300] = decimg[i]
except cv2.error:
create_img(i)
except TypeError:
pass
def send(conn,i,address):
global final
try:
print("sending")
while (True):
encoded = cv2.imencode(".jpg", final)[1].tobytes()
conn.sendto(encoded,address)
except socket.error:
conn.close()
def startsocket(count, conn):
vidsock[count] = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
vidsock[count].bind((myip, 0))
sendsock[count] = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
sendsock[count].bind((myip, 0))
conn.send(str(vidsock[count].getsockname()[1]).encode())
print("er")
data,vidadd[count] = vidsock[count].recvfrom(100)
print("er")
conn.send(str(sendsock[count].getsockname()[1]).encode())
print("er")
threadrec[count] = threading.Thread(target=receive, args=(sendsock[count], count,))
threadrec[count].start()
threadcreate[count] = threading.Thread(target=create_img, args=(count,))
threadcreate[count].start()
threadsnd[count] = threading.Thread(target=send, args=(vidsock[count],count,vidadd[count]))
threadsnd[count].start()
def forall(count):
sockall = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
sockall.bind((myip, 8888))
sockall.listen(100)
while (True):
conn[count], address = sockall.accept()
startsocket(count, conn[count])
count = count + 1
threadall = threading.Thread(target=forall, args=(0,))
threadall.start()