-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserverVideo.py
58 lines (48 loc) · 1.62 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
from socket import socket, AF_INET, SOCK_STREAM
from threading import Thread
import numpy as np
HOST = "192.168.43.215"
PORT = 5000
BufferSize = 4096
addresses = {}
def Connections():
while True:
client, addr = server.accept()
print("{} is connected!!".format(addr))
# client.send(("Welcome to Chat Room. Press {q} to exit").encode("utf-8"))
addresses[client] = addr
Thread(target=ClientConnection, args=(client, )).start()
def ClientConnection(client):
length = int(client.recv(2 * BufferSize).decode("utf-8"))
SendLength(client, str(length))
while True:
# print("In client Connections...")
receivingBuffer = client.recv(BufferSize)
if not receivingBuffer:
break
data = b''
if len(data) < length:
to_read = length - len(data)
data = client.recv(BufferSize if to_read > BufferSize else to_read)
length -= len(data)
broadcast(client, data)
def SendLength(clientSocket, length):
for client in addresses:
if client != clientSocket:
print("sending length:- {}".format(len(length)))
client.send(length.encode("utf-8"))
def broadcast(clientSocket, receivingBuffer):
for client in addresses:
if client != clientSocket:
# print("Broadcasting...")
client.send(receivingBuffer)
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()