-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserverVideo.py
76 lines (69 loc) · 2.16 KB
/
serverVideo.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
from socket import socket, AF_INET, SOCK_STREAM
from threading import Thread
import struct
HOST = input("Enter Host IP\n")
PORT = 3000
lnF = 640*480*3
CHUNK = 1024
addresses = {}
threads = {}
def Connections():
while True:
try:
client, addr = server.accept()
print("{} is connected!!".format(addr))
addresses[client] = addr
if len(addresses) > 1:
for sockets in addresses:
if sockets not in threads:
threads[sockets] = True
sockets.send(("start").encode())
Thread(target=ClientConnection, args=(sockets, )).start()
else:
continue
except:
continue
def ClientConnection(client):
while True:
try:
lengthbuf = recvall(client, 4)
length, = struct.unpack('!I', lengthbuf)
recvall(client, length)
except:
continue
def broadcast(clientSocket, data_to_be_sent):
for client in addresses:
if client != clientSocket:
client.sendall(data_to_be_sent)
def recvall(client, BufferSize):
databytes = b''
i = 0
while i != BufferSize:
to_read = BufferSize - i
if to_read > (1000 * CHUNK):
databytes = client.recv(1000 * CHUNK)
i += len(databytes)
broadcast(client, databytes)
else:
if BufferSize == 4:
databytes += client.recv(to_read)
else:
databytes = client.recv(to_read)
i += len(databytes)
if BufferSize != 4:
broadcast(client, databytes)
print("YES!!!!!!!!!" if i == BufferSize else "NO!!!!!!!!!!!!")
if BufferSize == 4:
broadcast(client, databytes)
return databytes
server = socket(family=AF_INET, type=SOCK_STREAM)
try:
server.bind((HOST, PORT))
except OSError:
print("Server Busy")
server.listen(2)
print("Waiting for connection..")
AcceptThread = Thread(target=Connections)
AcceptThread.start()
AcceptThread.join()
server.close()